package thread;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolDemo {
public static void main(String[] args){
long time1 = System.currentTimeMillis();
for(int i=0;i<1000;i++){
Thread t = new Thread(new OneRunnable());
t.start();
}
long time2 = System.currentTimeMillis();
System.out.println("不使用线程池花费的时间是:"+(time2-time1));

ExecutorService pool = Executors.newFixedThreadPool(1000);
time1 = System.currentTimeMillis();
for(int i=0;i<1000;i++){
pool.submit(new OneRunnable());
}
time2 = System.currentTimeMillis();
System.out.println("第一次使用线程池花费的时间是:"+(time2-time1));


time1 = System.currentTimeMillis();
for(int i=0;i<1000;i++){
pool.submit(new OneRunnable());
}
time2 = System.currentTimeMillis();
System.out.println("再次使用线程池花费的时间是:"+(time2-time1));

pool.shutdown();
}
}能说一下改错的原理吗

解决方案 »

  1.   

    Thread t = new Thread(new OneRunnable()); 这个?
    只是个测程序时间的代码~
      

  2.   

    Thread t = new Thread(new OneRunnable()); 
    说这行有错的
      
    后面的new OneRunnable()就跟着错Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    OneRunnable cannot be resolved to a type
    OneRunnable cannot be resolved to a type
    OneRunnable cannot be resolved to a type
      

  3.   

    不是没实现,是没有引入这个类~~
    看一下这OneRunnable这个类是在那定义的??
      

  4.   

    你是要计算调用1000次start的开销?还是要计算这1000个线程的运行时间?如果是前者,你的程序没什么问题。后者的话不太好办,因为统计本身也有开销的。
      

  5.   

    你里面的OneRunnable类大概是对Runnable接口的实现,但是你没有具体怎样实现接口的代码,所以会报错