Java实现指定线程执行顺序的三种方式示例
程序员文章站
2024-03-01 15:18:04
本文实例讲述了java实现指定线程执行顺序的三种方式。分享给大家供大家参考,具体如下:
方法一:通过共享对象锁加上可见变量来实现。
public class m...
本文实例讲述了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程序设计有所帮助。