WEB开发网
开发学院软件开发Java 关于 java.util.concurrent 您不知道的 5 件事,第... 阅读

关于 java.util.concurrent 您不知道的 5 件事,第 2 部分

 2010-07-12 00:00:00 来源:WEB开发网   
核心提示: 即使本例中的 10 个线程都在运行(您可以对运行 SemApp 的 Java 进程执行 jstack 来验证),但只有 3 个线程是活跃的,关于 java.util.concurrent 您不知道的 5 件事,第 2 部分(2),在一个信号计数器释放之前,其他 7 个线程都处于空闲状态,此类持有

即使本例中的 10 个线程都在运行(您可以对运行 SemApp 的 Java 进程执行 jstack 来验证),但只有 3 个线程是活跃的。在一个信号计数器释放之前,其他 7 个线程都处于空闲状态。(实际上,Semaphore 类支持一次获取和释放多个 permit,但这不适用于本场景。)

2. CountDownLatch

如果 Semaphore 是允许一次进入一个(这可能会勾起一些流行夜总会的保安的记忆)线程的并发性类,那么 CountDownLatch 就像是赛马场的起跑门栅。此类持有所有空闲线程,直到满足特定条件,这时它将会一次释放所有这些线程。

清单 2. CountDownLatch:让我们去赛马吧!

import java.util.*; 
import java.util.concurrent.*; 
 
class Race 
{ 
  private Random rand = new Random(); 
   
  private int distance = rand.nextInt(250); 
  private CountDownLatch start; 
  private CountDownLatch finish; 
   
  private List<String> horses = new ArrayList<String>(); 
   
  public Race(String... names) 
  { 
    this.horses.addAll(Arrays.asList(names)); 
  } 
   
  public void run() 
    throws InterruptedException 
  { 
    System.out.println("And the horses are stepping up to the gate..."); 
    final CountDownLatch start = new CountDownLatch(1); 
    final CountDownLatch finish = new CountDownLatch(horses.size()); 
    final List<String> places = 
      Collections.synchronizedList(new ArrayList<String>()); 
     
    for (final String h : horses) 
    { 
      new Thread(new Runnable() { 
        public void run() { 
          try 
          { 
            System.out.println(h + 
              " stepping up to the gate..."); 
            start.await(); 
             
            int traveled = 0; 
            while (traveled < distance) 
            { 
              // In a 0-2 second period of time.... 
              Thread.sleep(rand.nextInt(3) * 1000); 
               
              // ... a horse travels 0-14 lengths 
              traveled += rand.nextInt(15); 
              System.out.println(h + 
                " advanced to " + traveled + "!"); 
            } 
            finish.countDown(); 
            System.out.println(h + 
              " crossed the finish!"); 
            places.add(h); 
          } 
          catch (InterruptedException intEx) 
          { 
            System.out.println("ABORTING RACE!!!"); 
            intEx.printStackTrace(); 
          } 
        } 
      }).start(); 
    } 
 
    System.out.println("And... they're off!"); 
    start.countDown();     
 
    finish.await(); 
    System.out.println("And we have our winners!"); 
    System.out.println(places.get(0) + " took the gold..."); 
    System.out.println(places.get(1) + " got the silver..."); 
    System.out.println("and " + places.get(2) + " took home the bronze."); 
  } 
} 
 
public class CDLApp 
{ 
  public static void main(String[] args) 
    throws InterruptedException, java.io.IOException 
  { 
    System.out.println("Prepping..."); 
     
    Race r = new Race( 
      "Beverly Takes a Bath", 
      "RockerHorse", 
      "Phineas", 
      "Ferb", 
      "Tin Cup", 
      "I'm Faster Than a Monkey", 
      "Glue Factory Reject" 
      ); 
     
    System.out.println("It's a race of " + r.getDistance() + " lengths"); 
     
    System.out.println("Press Enter to run the race...."); 
    System.in.read(); 
     
    r.run(); 
  } 
} 

上一页  1 2 3 4  下一页

Tags:关于 java util

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接