照书上敲的一个例子,看不出哪里有问题,请教高手:
下面程序是用CountDownLatch来协调子线程的,但程序运行到begin.wait();的时候,一直处于阻塞状态,想了很久,还找不出原因。import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;public class Runner implements Callable<Integer>{
private CountDownLatch begin;
private CountDownLatch end;
public Runner(CountDownLatch begin,CountDownLatch end)
{
this.begin=begin;
this.end=end;
}
@Override
public Integer call() throws Exception {
// TODO Auto-generated method stub
int score=new Random().nextInt(25);
begin.wait();
TimeUnit.MILLISECONDS.sleep(score);
end.countDown();
return score;
}
public static void main(String[]args) throws InterruptedException, ExecutionException
{
int num=10;
CountDownLatch begin=new CountDownLatch(1);
CountDownLatch end=new CountDownLatch(num);
ExecutorService es=Executors.newFixedThreadPool(num);
List<Future<Integer>>futures=new ArrayList<Future<Integer>>();
for(int i=0;i<num;i++)
{
futures.add(es.submit(new Runner(begin,end)));
}
begin.countDown();
System.out.println("ffffffffffffffff");
end.await();
int count=0;
for(Future<Integer>f:futures){
count+=f.get();
}
System.out.println("平均分数为:"+count/num);
}
}
下面程序是用CountDownLatch来协调子线程的,但程序运行到begin.wait();的时候,一直处于阻塞状态,想了很久,还找不出原因。import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;public class Runner implements Callable<Integer>{
private CountDownLatch begin;
private CountDownLatch end;
public Runner(CountDownLatch begin,CountDownLatch end)
{
this.begin=begin;
this.end=end;
}
@Override
public Integer call() throws Exception {
// TODO Auto-generated method stub
int score=new Random().nextInt(25);
begin.wait();
TimeUnit.MILLISECONDS.sleep(score);
end.countDown();
return score;
}
public static void main(String[]args) throws InterruptedException, ExecutionException
{
int num=10;
CountDownLatch begin=new CountDownLatch(1);
CountDownLatch end=new CountDownLatch(num);
ExecutorService es=Executors.newFixedThreadPool(num);
List<Future<Integer>>futures=new ArrayList<Future<Integer>>();
for(int i=0;i<num;i++)
{
futures.add(es.submit(new Runner(begin,end)));
}
begin.countDown();
System.out.println("ffffffffffffffff");
end.await();
int count=0;
for(Future<Integer>f:futures){
count+=f.get();
}
System.out.println("平均分数为:"+count/num);
}
}
解决方案 »
- java菜鸟求助,关于判断输入是否非空
- email 标题长度
- 我的JSP页不能对数据库进行更新啊!!怎么办???
- 在线急等!求救json操作
- 一个简单的问题!
- 各位,我想学一些java数据库方面的编程,不知要安装配置什么。
- JAVA有没有象VB那样智能显示的平台啊???
- 通过函数可以取得文件名,不要扩展名?
- 请问:运行java程序时报Exception in thread "main" java.lang.NoClassDeFounError:HelloWorldApp错误(程序在下面)。是什么原因?
- 菜鸟问题:********如何在Javascript中使用ADO**********
- 新手请教一个简单程序,求解答
- java问题
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;public class Runner implements Callable<Integer>{
private CountDownLatch begin;
private CountDownLatch end;
public Runner(CountDownLatch begin,CountDownLatch end)
{
this.begin=begin;
this.end=end;
}
@Override
public Integer call() throws Exception {
// TODO Auto-generated method stub
int score=new Random().nextInt(25);
begin.await();
TimeUnit.MILLISECONDS.sleep(score);
end.countDown();
return score;
}
public static void main(String[]args) throws InterruptedException, ExecutionException
{
int num=10;
CountDownLatch begin=new CountDownLatch(1);
CountDownLatch end=new CountDownLatch(num);
ExecutorService es=Executors.newFixedThreadPool(num);
List<Future<Integer>>futures=new ArrayList<Future<Integer>>();
for(int i=0;i<num;i++)
{
futures.add(es.submit(new Runner(begin,end)));
}
begin.countDown();
System.out.println("ffffffffffffffff");
end.await();
int count=0;
for(Future<Integer>f:futures){
count+=f.get();
}
System.out.println("平均分数为:"+count/num);
}
}