高级并发编程Juc 1实现Callable接口实现多线程,这是除了继承Thread类和实现Runnable方法的第三种方式,入门阶段只做了解即可
package yzy.cn;
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 implementCallable implements Callable<Boolean>{
private String url;
private String name; //存储路径
public implementCallable(String url, String name) {
super();
this.url = url;
this.name = name;
}
/*
* Callbale的call方法是线程体,且具有返回值
*/
@Override
public Boolean call() throws Exception {
webDownloader wd = new webDownloader();
wd.download(url, name);
System.out.println(name);
return true;
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
implementCallable icd1 = new implementCallable(
"https://img-blog.csdnimg.cn/20200920094607231.png",
"PrintStream继承关系.png");
implementCallable icd2 = new implementCallable(
"https://img-blog.csdnimg.cn/20200920104317535.png",
"PrintWriter继承关系.png");
implementCallable icd3 = new implementCallable(
"https://img-blog.csdnimg.cn/20200917153006360.png",
"ObjectInputStream继承关系.png");
implementCallable icd4 = new implementCallable(
"https://img-blog.csdnimg.cn/20200917153309352.png",
"ObjectOutputStream继承关系.png");
//创建执行服务
ExecutorService service = Executors.newFixedThreadPool(4); //java.util.concurrent
//提交执行
Future<Boolean> resultFuture1 = service.submit(icd1);
Future<Boolean> resultFuture2 = service.submit(icd2);
Future<Boolean> resultFuture3 = service.submit(icd3);
Future<Boolean> resultFuture4 = service.submit(icd4);
//获取结果
boolean r1 = resultFuture1.get();
boolean r2 = resultFuture2.get();
boolean r3 = resultFuture3.get();
boolean r4 = resultFuture4.get();
//释放资源、关闭服务
service.shutdownNow();
}
}
相较于实现Runnable实现多线程的覆盖无返回值的run方法外;实现Callable接口实现多线程,重写的call方法是有返回值的,其返回值类型和Callable的模板返回值类型相同,线程的启动过程有:创建执行服务、提交执行、获取结果、释放资源、关闭服务
以上四线程下载网络URL图片资源的结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/c1b64fe7ba39ababcb46aaa7fe362492.png)
都下载成功了
为了加深我们对实现Callable接口的使用印象,我们对前面博客中的龟兔赛跑,进行改造
package yzy.cn;
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 racerWithCallable implements Callable<Integer> {
private static String winner;
@Override
public Integer call()throws Exception {
for(int steps=1; steps<=100; ++steps) {
if(Thread.currentThread().getName() == "pool-1-thread-1" && steps % 10 == 0)
Thread.sleep(10);
System.out.println(Thread.currentThread().getName() + " " + steps);
if(gameOver(steps))
return steps;
}
return null;
}
private boolean gameOver(int steps) {
if(winner != null) {
return true;
}else {
if(steps == 100) {
winner = Thread.currentThread().getName();
System.out.println("winner-->"+winner);
return true;
}
}
return false;
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws InterruptedException, ExecutionException{
racer r = new racer();
//创建执行服务
ExecutorService service = Executors.newFixedThreadPool(2); //java.util.concurrent
//提交执行
Future<Integer> resultFuture1 = (Future<Integer>) service.submit(r);
Future<Integer> resultFuture2 = (Future<Integer>) service.submit(r);
//获取结果
Integer r1 = resultFuture1.get();
Integer r2 = resultFuture2.get();
System.out.println(r1+"-_-"+r2);
//释放资源、关闭服务
service.shutdownNow();
}
}
java.util.concurrent ↩︎