134.高级并发编程Juc实现Callable接口实现多线程

高级并发编程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图片资源的结果如下:

都下载成功了

为了加深我们对实现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();
	}
}



  1. java.util.concurrent ↩︎

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值