赞
踩
- public class Cyclewait implements Runnable {
- private String value;
-
- @Override
- public void run() {
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- value = "we have data now";
- }
-
- public static void main(String[] args) throws InterruptedException {
- Cyclewait cyclewait = new Cyclewait();
- Thread thread = new Thread(cyclewait);
- thread.start();
- // 主线程等待法,核心代码:当value为空时循环等待
- while (null == cyclewait.value) {
- Thread.sleep(100);
- }
- System.out.println("value:" + cyclewait.value);
- }
- }
运行结果:
value:we have data now
缺点:需要自己实现循环等待的逻辑,当需要等待的变量很多时,代码会显得异常臃肿;同时需要循环多久是未知的,无法做到更精准的控制
- public class Cyclewait implements Runnable {
- private String value;
-
- @Override
- public void run() {
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- value = "we have data now";
- }
-
- public static void main(String[] args) throws InterruptedException {
- Cyclewait cyclewait = new Cyclewait();
- Thread thread = new Thread(cyclewait);
- thread.start();
- // 使用Thread类的join()阻塞当前线程以等待子线程处理完毕
- thread.join();
- System.out.println("value:" + cyclewait.value);
- }
- }
运行结果:
value:we have data now
Thread类的join()方法会阻塞调用此方法的线程,即这里会阻塞主线程,直到join()方法所在的线程执行完毕为止
缺点:无法处理更精准的依赖关系
实现Callable接口
- import java.util.concurrent.Callable;
-
- public class MyCallable implements Callable<String> {
-
- @Override
- public String call() throws Exception {
- String value = "test";
- System.out.println("Ready to work");
- Thread.sleep(5000);
- System.out.println("task done");
- return value;
- }
- }
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.FutureTask;
-
- public class FutureTaskDemo {
- public static void main(String[] args) throws ExecutionException, InterruptedException {
- FutureTask<String> futureTask=new FutureTask<String>(new MyCallable());
- new Thread(futureTask).start();
- if(!futureTask.isDone()){
- System.out.println("task has not finished,please wait!");
- }
- System.out.println("task return:"+futureTask.get());
- }
- }
运行结果:
- task has not finished,please wait!
- Ready to work
- task done
- task return:test
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
-
- public class ThreadPoolDemo {
- public static void main(String[] args) {
- ExecutorService executorService = Executors.newCachedThreadPool();
- Future<String> future = executorService.submit(new MyCallable());
- if (!future.isDone()) {
- System.out.println("task has not finished,please wait!");
- }
- try {
- System.out.println("task return:" + future.get());
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- } finally {
- executorService.shutdown();
- }
- }
- }
运行结果:
- task has not finished,please wait!
- Ready to work
- task done
- task return:test
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。