Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式示例
本文實(shí)例講述了Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式。分享給大家供大家參考,具體如下:
方法一:通過(guò)共享對(duì)象鎖加上可見變量來(lái)實(shí)現(xiàn)。
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();
}
}
}
執(zhí)行結(jié)果:

可以看到線程的啟動(dòng)按順序執(zhí)行了。共享對(duì)象鎖,可以保證每個(gè)方法只能同時(shí)有一個(gè)線程進(jìn)入,配合wait和notifyall方法,可以啟動(dòng)或者喚醒線程。
方法二:通過(guò)主線程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();
}
}
方法三:通過(guò)線程執(zhí)行時(shí)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相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java進(jìn)程與線程操作技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Jmeter生成UUID作為唯一標(biāo)識(shí)符過(guò)程圖解
這篇文章主要介紹了Jmeter生成UUID作為唯一標(biāo)識(shí)符過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
不規(guī)范使用ThreadLocal導(dǎo)致bug分析解決
這篇文章主要為大家介紹了不規(guī)范使用ThreadLocal導(dǎo)致bug分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
實(shí)例解析Java設(shè)計(jì)模式編程中的適配器模式使用
本篇文章主要通過(guò)實(shí)例對(duì)適配器模式進(jìn)行了詳解,需要的朋友可以參考下2017-04-04
java實(shí)現(xiàn)socket客戶端連接服務(wù)端
本文是個(gè)人剛剛開始學(xué)習(xí)如何通過(guò)socket去發(fā)送信息下邊的案例,也是書上的在這留下筆記,最后附上一個(gè)實(shí)例,有需要的小伙伴可以參考下。2015-10-10
spring boot實(shí)現(xiàn)驗(yàn)證碼功能
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過(guò)程。這篇文章主要介紹了spring boot實(shí)現(xiàn)驗(yàn)證碼功能,需要的朋友可以參考下2018-04-04
Java設(shè)計(jì)模式編程中的責(zé)任鏈模式使用示例
這篇文章主要介紹了Java設(shè)計(jì)模式編程中的責(zé)任鏈模式使用示例,責(zé)任鏈模式可以避免很多請(qǐng)求的發(fā)送者和接收者之間的耦合關(guān)系,需要的朋友可以參考下2016-05-05
Java正則驗(yàn)證字串符RegexValidator類使用
正則驗(yàn)證字串符是一種強(qiáng)大的工具,可以幫助程序員在處理字符串時(shí)輕松進(jìn)行復(fù)雜匹配,本文將介紹正則表達(dá)式的概念、語(yǔ)法和在編程中的應(yīng)用,并通過(guò)實(shí)例演示如何使用正則表達(dá)式進(jìn)行字符串匹配、替換和提取等操作2023-11-11

