直接上代码
package com.gunsmoke;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadTest {
public static void main(String[] args) {
ExecutorService pool = Executors.newCachedThreadPool();
Task task = new Task();
for(int index = 0; index < 100; index++)
{
pool.submit(task);
}
}
}
class Task implements Runnable
{
@Override
public void run()
{
System.out.println("当前线程:" + Thread.currentThread().getName());
}
}
运行结果(截取一部分)
当前线程:pool-1-thread-1
当前线程:pool-1-thread-4
当前线程:pool-1-thread-3
当前线程:pool-1-thread-2
当前线程:pool-1-thread-5
当前线程:pool-1-thread-6
当前线程:pool-1-thread-3
当前线程:pool-1-thread-6
当前线程:pool-1-thread-2
当前线程:pool-1-thread-4
当前线程:pool-1-thread-1
可以看到有的线程被反复使用