//callable 和 runnable的区别:
//runnable 的run方法不会有任何返回结果。所以祝线程无法获得任务线程的返回值
//callable的call方法可以返回结果,但是主线程在获取时是被阻塞的,需要等待任务线程返回才能拿到结果。
package threadimp;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MyThreadCachePool {
public static void main(String[] args) {
ExecutorService pool = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
pool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " start");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
pool.shutdown();
}
}
package threadimp;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class MyThreadFixPoolCallable {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(4);
for (int i = 0; i < 8; i++) {
Future<String> result = pool.submit(new Callable<String>() {
@Override
public String call() {
System.out.println(Thread.currentThread().getName() + " start");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Thread.currentThread().getName();
}
});
try {
System.out.println(result.get());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
pool.shutdown();
}
}
//result.get() 会阻塞!!!
package threadimp;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TaskPool {
public static void main(String[] args) {
Future<?> submit = null;
Random random = new Random();
ExecutorService exec = Executors.newFixedThreadPool(4);
ArrayList<Future<?>> results = new ArrayList<>();
for (int i = 0; i < 10; i++) {
submit = exec.submit(new TaskCallable(i));
results.add(submit);
}
for (Future f: results) {
boolean done = f.isDone();
System.out.println(done);
try {
System.out.println("result " + f.get());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package threadimp;
import java.util.Random;
import java.util.concurrent.Callable;
public class TaskCallable implements Callable<String> {
private int s;
Random r = new Random();
public TaskCallable(int s) {
this.s = s;
}
@Override
public String call() {
int rand = r.nextInt(3);
String name = Thread.currentThread().getName();
System.out.println(name + " start");
try {
Thread.sleep(rand * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return name + " " + s;
}
}
thread pool
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- thread pool hell this post content some of the basics of ...
- 问题背景 今天下午,突然收到nginx告警邮件,线上服务的web有N多的错误. 解决步骤 第一步 立马保存现场, ...
- 更多 Java 并发编程方面的文章,请参见文集《Java 并发编程》 线程组 Thread Group 线程的集合...
- 使用Xcode 9之后发现控制台一直在打印这个警告, 大致意思就是UI没在主线程刷新 image.png 出现类似...