设置三个线程顺序打印数字问题(转载)

本文介绍三种Java中控制线程执行顺序的方法:使用共享对象锁、主线程调用join方法及线程间调用join方法。这些技巧对于实现精确的线程同步非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种方法:通过共享对象锁的方式实现
记住调用wait方法时候需要在同步代码块内,否则会报java.lang.IllegalMonitorStateException异常!

package com.test;

class Prints {
    public int num = 0;
    synchronized public void methodA() throws InterruptedException{
        while(num!=0){
            wait();
        }
        System.out.println(Thread.currentThread().getName()+":"+num);
        num = 1;
        notifyAll();
    }
    synchronized public void methodB() throws InterruptedException{
        while(num!=1){
            wait();
        }
        System.out.println(Thread.currentThread().getName()+":"+num);
        num = 2;
        notifyAll();
    }
    synchronized public void methodC() throws InterruptedException{
        while(num!=2){
            wait();
        }
        System.out.println(Thread.currentThread().getName()+":"+num);
        notifyAll();
    }
}
class ThreadClassA implements Runnable{
    private Prints p;
    public ThreadClassA(Prints p) {
        this.p = p;
    }
    @Override
    public void run() {
        try {
            p.methodA();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
class ThreadClassB implements Runnable{
    private Prints p;
    public ThreadClassB(Prints p) {
        this.p = p;
    }
    @Override
    public void run() {
        try {
            p.methodB();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
class ThreadClassC implements Runnable{
    private Prints p;
    public ThreadClassC(Prints p) {
        this.p = p;
    }
    @Override
    public void run() {
        try {
            p.methodC();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
public class MainDemo{
    public static void main(String[] args) throws CloneNotSupportedException {
        Prints p = new Prints();
        ThreadClassA a = new ThreadClassA(p);
        ThreadClassB b = new ThreadClassB(p);
        ThreadClassC c = new ThreadClassC(p);
        Thread t1 = new Thread(a);
        Thread t2 = new Thread(b);
        Thread t3 = new Thread(c);
        t1.start();
        t2.start();
        t3.start();
    }
}

第二种方法:通过主线程Join(),join的理解:https://www.cnblogs.com/lcplcpjava/p/6896904.html

package com.test;

class Prints {
    public int num = 0;
    synchronized public void methodA() throws InterruptedException{
        while(num!=0){
            wait();
        }
        System.out.println(Thread.currentThread().getName()+":"+num);
        num = 1;
        notifyAll();
    }
    synchronized public void methodB() throws InterruptedException{
        while(num!=1){
            wait();
        }
        System.out.println(Thread.currentThread().getName()+":"+num);
        num = 2;
        notifyAll();
    }
    synchronized public void methodC() throws InterruptedException{
        while(num!=2){
            wait();
        }
        System.out.println(Thread.currentThread().getName()+":"+num);
        notifyAll();
    }
}
class ThreadClassA implements Runnable{
    private Prints p;
    public ThreadClassA(Prints p) {
        this.p = p;
    }
    @Override
    public void run() {
//      synchronized (Thread.currentThread()) {
            System.out.println("1");
//      }
/*      try {
            p.methodA();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    }

}
class ThreadClassB implements Runnable{
    private Prints p;
    public ThreadClassB(Prints p) {
        this.p = p;
    }
    @Override
    public void run() {
        System.out.println("2");
/*      try {
            p.methodB();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    }

}
class ThreadClassC implements Runnable{
    private Prints p;
    public ThreadClassC(Prints p) {
        this.p = p;
    }
    @Override
    public void run() {
        System.out.println("3");
/*      try {
            p.methodC();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    }

}
public class MainDemo{
    public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
        Prints p = new Prints();
        ThreadClassA a = new ThreadClassA(p);
        ThreadClassB b = new ThreadClassB(p);
        ThreadClassC c = new ThreadClassC(p);
        Thread t1 = new Thread(a);
        Thread t2 = new Thread(b);
        Thread t3 = new Thread(c);
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
        t3.join();
    }
}

第三种:通过线程执行时调用join方法

package com.test;

import java.util.Random;

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 MainDemo{
    public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
            T1 t1 = new T1();
            T2 t2 = new T2(t1);
            T3 t3 = new T3(t2);
            t2.start();
            t1.start();
            t3.start();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值