| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | packagecom.test.thread;publicclassCycleWait implementsRunnable {    privateString value;    @Override    publicvoidrun() {        try{            Thread.sleep(5000);        } catch(InterruptedException e) {            e.printStackTrace();        }        value = "we have data now";    }    publicstaticvoidmain(String[] args) throwsInterruptedException {        CycleWait cycleWait = newCycleWait();        Thread t = newThread(cycleWait);        t.start();        while(cycleWait.value == null) {            Thread.sleep(100);        }        System.out.println(cycleWait.value);    }} | 
运行结果:
| 1 | we have data now | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | packagecom.test.thread;publicclassCycleWait implementsRunnable {    privateString value;    @Override    publicvoidrun() {        try{            Thread.sleep(5000);        } catch(InterruptedException e) {            e.printStackTrace();        }        value = "we have data now";    }    publicstaticvoidmain(String[] args) throwsInterruptedException {        CycleWait cycleWait = newCycleWait();        Thread t = newThread(cycleWait);        t.start();       // join方法,在start后        t.join();        System.out.println(cycleWait.value);    }} | 
1、future task
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | MyCallable.class:packagecom.test.thread;importjava.util.concurrent.Callable;// 实现callable接口publicclassMyCallable implementsCallable<String> {    @Override    publicString call() throwsException {        String value = "test";        System.out.println("ready to work");        Thread.sleep(5000);        System.out.println("task done");        returnvalue;    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | FutureTaskDemo.class:packagecom.test.thread;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.FutureTask;//future taskpublicclassFutureTaskDemo {    publicstaticvoidmain(String[] args) throwsExecutionException, InterruptedException {        FutureTask<String> task = newFutureTask<String>(newMyCallable());        newThread(task).start();        if(!task.isDone()) {            System.out.println("task has not finished, please wait!");        }        System.out.println("task return:"+ task.get());    }} | 
2、线程池
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | TreadPoolDemo.class:packagecom.test.thread;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Future;publicclassTreadPoolDemo {    publicstaticvoidmain(String[] args) {        // 创建线程池        ExecutorService executorService =                Executors.newCachedThreadPool();        // 向线程池中提交任务        Future<String> future = executorService.submit(newMyCallable());        // 判断任务是否完成        if(!future.isDone()) {            System.out.println("task not finished, please wait~~");        }        try{            System.out.println(future.get());        } catch(InterruptedException e) {            e.printStackTrace();        } catch(ExecutionException e) {            e.printStackTrace();        } finally{            // 将线程池关闭            executorService.shutdown();        }    }} | 
原文:https://www.cnblogs.com/telwanggs/p/13979964.html