/**
1.Runable()
public class ThreadPoolDemo {
public static void main(String[] args)
{
LinkedBlockingQueue objects = new LinkedBlockingQueue<>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,20,3,TimeUnit.SECONDS,objects);
//100个任务
for(int i = 0 ; i<100; i++)
{
threadPoolExecutor.submit(()->{
System.out.println(Thread.currentThread().getName());
});
}
}
2.Callable
public class CallableDemo implements Callable {
@Override
public String call() throws Exception {
Thread.sleep(3000);
return "111";
}
public static void main(String[] args) throws Exception{
CallableDemo callableDemo = new CallableDemo();
FutureTask stringFutureTask = new FutureTask<>(callableDemo);
new Thread(stringFutureTask).start();
System.out.println(stringFutureTask.get());
}
}
2.1
public class ThreadPoolDemoPlusCallable {
/**
* 希望获取到每个线程返回一个结果就用 Callable
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
LinkedBlockingQueue objects = new LinkedBlockingQueue<>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,20,3,TimeUnit.SECONDS,objects);
Future submit = null;
//100个任务
for(int i = 0 ; i<100; i++)
{
submit = threadPoolExecutor.submit(new CallableDemo());
}
for(int i = 0 ; i<100; i++)
{
System.out.println(submit.get());
}
}
}
}