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

第一种方法:通过共享对象锁的方式实现
记住调用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();

    }
}

你可能感兴趣的:(java编程思想)