如何控制线程执行的顺序?
程序员文章站
2022-05-05 21:46:23
...
方法一:通过共享对象锁加上可见变量来实现。
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()
thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。
比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。
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();
/**join的意思是使得放弃当前线程的执行,并返回对应的线程,例如下面代码的意思就是:
程序在main线程中调用t1线程的join方法,则main线程放弃cpu控制权,并返回t1线程继续执行直到线程t1执行完毕
所以结果是t1线程执行完后,才到主线程执行,相当于在main线程中同步t1线程,t1执行完了,main线程才有执行的机会
*/
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();
}
}
上一篇: 如何保证线程的执行顺序