赞
踩
掌握同步操作,实现资源最大程度利用
掌握三种回电方式
1 while循环等待
2 join
3 implements Callable
Executors,Future,get,Callable,Runnable
以空间换时间的方式解决问题。
import java.util.ArrayList; import java.util.concurrent.*; /** * 多线程返回值的三种方式 * 1 while循环等待 * 2 join * 3 implements Callable * Created by xiaojinlu1990@163.com on 2020/11/29 0029. */ public class ExecutorDemo { public static void main(String[] args) throws InterruptedException, ExecutionException { //第一种 循环主线程等待法 TestTreadFrist testTreadFrist = new TestTreadFrist(); testTreadFrist.setNum("test1"); Thread thread1 = new Thread(); thread1.start(); while (testTreadFrist.getNum()==null){ Thread.sleep(1000); } System.out.println(testTreadFrist.getNum()); //第二种 join等待法 TestTreadTwe testTreadTwe = new TestTreadTwe(); testTreadTwe.setNum("test2"); Thread thread2 = new Thread(); thread2.start(); thread2.join();//join等待法 System.out.println(testTreadTwe.getNum()); //第三种1 实现Callable 获取返回值 ExecutorService service = Executors.newCachedThreadPool(); TestTreadThree testTreadThree = new TestTreadThree(); Future<String> str = service.submit(testTreadThree); System.out.println(str.get()); //第三种2 多回调返回 + 超时时间版本 ExecutorDemo executorDemo = new ExecutorDemo(); executorDemo.getThread(); } public void getThread() { ExecutorService service = Executors.newCachedThreadPool(); ArrayList<Future<String>> results = new ArrayList<Future<String>>(); for (int i = 0; i < 10; i++) { TestTread testTread = new TestTread(); testTread.setNum(String.valueOf(i)); results.add(service.submit(testTread)); } for(Future<String> fs : results){ try { System.out.println(""); long timeOut =2; System.out.println(fs.get(timeOut, TimeUnit.SECONDS)); } catch (InterruptedException e) { e.printStackTrace(); return ; } catch (ExecutionException e) { e.printStackTrace(); } catch (TimeoutException e) { //打印超时 e.printStackTrace(); } finally{ service.shutdown(); } } } } class TestTread implements Callable{ private String num; public void setNum(String num) { this.num = num; } public String getNum() { return num; } @Override public Object call() throws Exception { if(num.equals("3")){ Thread.sleep(10000); }else { Thread.sleep(1900); } System.out.printf(num); return getNum(); } } //第一种 class TestTreadFrist implements Runnable{ private String num; public void setNum(String num) { this.num = num; } public String getNum() { return num; } @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } setNum("------"); } } //第二种 class TestTreadTwe implements Runnable{ private String num; public void setNum(String num) { this.num = num; } public String getNum() { return num; } @Override public void run() { } } //第三种 实现Callable class TestTreadThree implements Callable{ private String num; public void setNum(String num) { this.num = num; } public String getNum() { return num; } @Override public Object call() throws Exception { setNum("test3"); return getNum(); } }
根据场景对用适应才最好,最适宜的就是最好的。
java API
https://tool.oschina.net/apidocs/apidoc?api=jdk-zh
多留言多点赞你们的只支持是我坚持下去的动力,都支棱起来!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。