一道经典的Java多线程编程题

问题描述
启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20….以此类推, 直到打印到75. 程序的输出结果应该为:

线程1: 1
线程1: 2
线程1: 3
线程1: 4
线程1: 5

线程2: 6
线程2: 7
线程2: 8
线程2: 9
线程2: 10

线程3: 71
线程3: 72
线程3: 73
线程3: 74
线程3: 75

解法一:

class PrintRunnable implements Runnable {

    private static volatile int printNum = 0;
    private int threadId;
    private Object o;
    public PrintRunnable(int threadId,Object o){
        this.threadId = threadId;
        this.o=o;
    }
    @Override
    public void run() {
        while(printNum < 75){
            synchronized (o){
                if (printNum/5%3 + 1 == threadId){
                //该判断确保对应线程输出
                    for (int i = 0; i <5; i++) {
                        System.out.println("线程"+threadId+":"+(++printNum));
                    }
                    o.notifyAll();
                }else {
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        new Thread(new PrintRunnable(1,o)).start();
        new Thread(new PrintRunnable(2,o)).start();
        new Thread(new PrintRunnable(3,o)).start();
    }
}

解法二:

public class Print123456 implements Runnable{
    private String name;
    private Object prev;
    private Object self;
    private int count;
    public Print123456(String name,Object prev,Object self,int count){
        this.name=name;
        this.prev=prev;
        this.self=self;
        this.count=count;
    }
    @Override
    public void run() {
        while(count<=75){
            synchronized (prev){
                synchronized (self){
                    for (int i=0;i<5;i++){
                        count=count+1;
                        if(count>75)break;
                        System.out.println(name+": "+count);

                    }
                    count=count+10;
                    //System.out.println("释放自身锁,并唤醒在等待该锁的线程");
                    self.notify();//释放自身锁,并唤醒在等待该锁的线程

                }
                //System.out.println("释放自身锁成功");
                try {
                    //System.out.println("等待前一个锁");
                    prev.wait();//等待前一个锁

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //System.out.println("两个锁全部释放,重新进入新的循环,但是由于有prev.wait()所以该线程等待获取prev的锁");
        }
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Object a=new Object();
        Object b=new Object();
        Object c=new Object();
        Print123456 A=new Print123456("线程1",c,a,0);
        Print123456 B=new Print123456("线程2",a,b,5);
        Print123456 C=new Print123456("线程3",b,c,10);
        new Thread(A).start();
        Thread.sleep(100);
        new Thread(B).start();
        Thread.sleep(100);
        new Thread(C).start();
        Thread.sleep(100);
    }
}

你可能感兴趣的:(java)