时间:2021-05-20
本文实例讲述了Java实现指定线程执行顺序的三种方式。分享给大家供大家参考,具体如下:
方法一:通过共享对象锁加上可见变量来实现。
public class MyService { private volatile int orderNum = 1; public synchronized void methodA() { try { while (orderNum != 1) { wait(); } for (int i = 0; i < 2; i++) { System.out.println("AAAAA"); } orderNum = 2; notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void methodB() { try { while (orderNum != 2) { wait(); } for (int i = 0; i < 2; i++) { System.out.println("BBBBB"); } orderNum = 3; notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void methodC() { try { while (orderNum != 3) { wait(); } for (int i = 0; i < 2; i++) { System.out.println("CCCCC"); } orderNum = 1; notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } }}import service.MyService;public class ThreadAA extends Thread { private MyService dbtools; public ThreadAA(MyService dbtools) { super(); this.dbtools = dbtools; } @Override public void run() { dbtools.methodA(); }}import service.MyService;public class ThreadBB extends Thread { private MyService dbtools; public ThreadBB(MyService dbtools) { super(); this.dbtools = dbtools; } @Override public void run() { dbtools.methodB(); }}import service.MyService;public class ThreadCC extends Thread { private MyService dbtools; public ThreadCC(MyService dbtools) { this.dbtools = dbtools; } @Override public void run() { dbtools.methodC(); }}import extthread.ThreadCC;import service.MyService;import extthread.ThreadAA;import extthread.ThreadBB;public class Run { public static void main(String[] args) { MyService myService = new MyService(); for (int i = 0; i < 2; i++) { ThreadBB output = new ThreadBB(myService); output.start(); ThreadAA input = new ThreadAA(myService); input.start(); ThreadCC threadCC = new ThreadCC(myService); threadCC.start(); } }}执行结果:
可以看到线程的启动按顺序执行了。共享对象锁,可以保证每个方法只能同时有一个线程进入,配合wait和notifyall方法,可以启动或者唤醒线程。
方法二:通过主线程Join()
class T11 extends Thread { public void run() { System.out.println("in T1"); }}class T22 extends Thread { public void run() { System.out.println("in T2"); }}class T33 extends Thread { public void run() { System.out.println("in T3"); }}public class Test2 { public static void main(String[] args) throws InterruptedException { T11 t1 = new T11(); T22 t2 = new T22(); T33 t3 = new T33(); t1.start(); t1.join(); t2.start(); t2.join(); t3.start(); }}方法三:通过线程执行时Join()
class T1 extends Thread { public void run(){ Random random = new Random(); try { Thread.sleep(random.nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("in T1"); }}class T2 extends Thread{ private Thread thread; public T2(Thread thread) { this.thread = thread; } public void run(){ try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("in T2"); }}class T3 extends Thread{ private Thread thread; public T3(Thread thread) { this.thread = thread; } public void run(){ try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("in T3"); }}public class Test { public static void main(String[] args) throws InterruptedException { T1 t1 = new T1(); T2 t2 = new T2(t1); T3 t3 = new T3(t2); t2.start(); t1.start(); t3.start(); }}更多java相关内容感兴趣的读者可查看本站专题:《Java进程与线程操作技巧总结》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Java创建线程主要有三种方式:继承Thread类创建线程、实现Runnable接口创建线程和实现Callable和Future创建线程。继承Thread类pu
Java多线程实例3种实现方法Java中的多线程有三种实现方式:1.继承Thread类,重写run方法。Thread本质上也是一个实现了Runnable的实例,
在Android开发中,通过以下三种方法定时执行任务:一、采用Handler与线程的sleep(long)方法(不建议使用,java的实现方式)二、采用Hand
java实现多线程的三种方法在java中,有三种方法可以实现多线程。第一种方法:继承Thread类,重写run函数。第二种方法:实现Runnable接口,重写r
Java线程会议如下三种方式结束,结束后就处于死亡状态1、run()或者call()方法执行完成,线程正常结束;2、线程抛出一个未捕获的Exception或Er