赞
踩
public class ThreadOrder { public static List<Integer> list = new ArrayList<>(); public static void main(String[] args) { //1、通过监测信号 //每个线程对应自己的一个信号,监测到信号就执行,执行完就将信号更改为下一个线程的信号 Thread t11 = new Thread("通过监测信号 : 0"){ @Override public void run() { while (list.get(0) != 0){ try {Thread.sleep(500);}catch (Exception ignore){} } System.out.println(super.getName() + " : 执行任务"); list.remove(0); } }; Thread t12 = new Thread("通过监测信号 : 1"){ @Override public void run() { while (list.get(0) != 1){ try {Thread.sleep(500);}catch (Exception ignore){} } System.out.println(super.getName() + " : 执行任务"); list.remove(0); } }; Thread t13 = new Thread("通过监测信号 : 2"){ @Override public void run() { while (list.get(0) != 2){ try {Thread.sleep(500);}catch (Exception ignore){} } System.out.println(super.getName() + " : 执行任务"); list.remove(0); } }; list.add(0); list.add(1); list.add(2); t11.start(); t12.start(); t13.start(); //2、通过FutureTask可以阻塞获取返回值的特性 FutureTask<String> task1 = new FutureTask<>(new Callable<String>() { @Override public String call() throws Exception { System.out.println(Thread.currentThread().getName() + " : 执行任务"); return Thread.currentThread().getName() + " : 返回值"; } }); Thread t21 = new Thread(task1,"通过FutureTask : 0"); FutureTask<String> task2 = new FutureTask<>(new Callable<String>() { @Override public String call() throws Exception { System.out.println(Thread.currentThread().getName() + " : 执行任务"); return Thread.currentThread().getName() + " : 返回值"; } }); Thread t22 = new Thread(task2,"通过FutureTask : 1"); FutureTask<String> task3 = new FutureTask<>(new Callable<String>() { @Override public String call() throws Exception { System.out.println(Thread.currentThread().getName() + " : 执行任务"); return Thread.currentThread().getName() + " : 返回值"; } }); Thread t23 = new Thread(task3,"通过FutureTask : 2"); t21.start(); try {System.out.println(task1.get());}catch (Exception ignore){} t22.start(); try {System.out.println(task2.get());}catch (Exception ignore){} t23.start(); try {System.out.println(task3.get());}catch (Exception ignore){} //3、通过join()阻塞等待线程执行完 Thread t31 = new Thread("通过join()阻塞 : 0"){ @Override public void run() { try {Thread.sleep(1000);}catch (Exception ignore){} System.out.println(super.getName() + " : 执行任务"); } }; Thread t32 = new Thread("通过join()阻塞 : 1"){ @Override public void run() { try {Thread.sleep(1000);}catch (Exception ignore){} System.out.println(super.getName() + " : 执行任务"); } }; Thread t33 = new Thread("通过join()阻塞 : 2"){ @Override public void run() { try {Thread.sleep(1000);}catch (Exception ignore){} System.out.println(super.getName() + " : 执行任务"); } }; t31.start(); try {t31.join();}catch (Exception ignore){} t32.start(); try {t32.join();}catch (Exception ignore){} t33.start(); try {t33.join();}catch (Exception ignore){} //4、通过单线程线程池特性 ExecutorService pool = Executors.newSingleThreadExecutor(); Thread t41 = new Thread("通过单线程线程池 : 0"){ @Override public void run() { try {Thread.sleep(1000);}catch (Exception ignore){} System.out.println(super.getName() + " : 执行任务"); } }; Thread t42 = new Thread("通过单线程线程池 : 1"){ @Override public void run() { try {Thread.sleep(1000);}catch (Exception ignore){} System.out.println(super.getName() + " : 执行任务"); } }; Thread t43 = new Thread("通过单线程线程池 : 2"){ @Override public void run() { try {Thread.sleep(1000);}catch (Exception ignore){} System.out.println(super.getName() + " : 执行任务"); } }; pool.submit(t41); pool.submit(t42); pool.submit(t43); pool.shutdown(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。