启动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…

启动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:

最常规死板的做法,缺点是有多少中线程同步就需要建多少中函数,所以本题需要建3个函数,其次对于打印到75这点需要自己算好循环次数,本题每种线程循环5次即最后到75

public class Main {

    public static void main(String[] args){
        MyObj myObj = new MyObj();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0; i<5; i++)
                    myObj.print1();
            }
        },"A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0; i<5; i++)
                    myObj.print2();
            }
        },"B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0; i<5; i++)
                    myObj.print3();
            }
        },"C").start();
    }

}

class MyObj{
    private static int flag = 0;
    private static int num = 1;

    public synchronized void print1(){
        while(flag != 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.print(Thread.currentThread().getName() + ": ");
        for(int i=0; i<5; i++, num++)
            System.out.print(num+", ");
        System.out.println();
        
        flag++;
        this.notifyAll();
    }

    public synchronized void print2(){
        while(flag != 1){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        System.out.print(Thread.currentThread().getName() + ": ");
        for(int i=0; i<5; i++, num++)
            System.out.print(num+", ");
        System.out.println();
        
        flag++;
        this.notifyAll();
    }

    public synchronized void print3(){
        while(flag != 2){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        System.out.print(Thread.currentThread().getName() + ": ");
        for(int i=0; i<5; i++, num++)
            System.out.print(num+", ");
        System.out.println();
        
        flag = 0;
        this.notifyAll();
    }
}

结果:

启动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…_第1张图片

解题思路2:

简化版

public class Main {

    public static void main(String[] args){
        Object o = new Object();

        new Thread(new PrintRunnable(o, 1)).start();
        new Thread(new PrintRunnable(o, 2)).start();
        new Thread(new PrintRunnable(o, 3)).start();
    }

}

class PrintRunnable implements Runnable{
    private final Object o;
    private int threadId;
    private static volatile int num = 1;

    PrintRunnable(Object o, int threadId){
        this.o = o;
        this.threadId = threadId;
    }

    @Override
    public void run() {
        while(num < 65){
            synchronized (o){
                while(num/5%3 + 1 != threadId){
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }


                System.out.print("线程"+threadId+": ");
                for(int i=0; i<5; i++, num++)
                    System.out.print(num + ",");
                System.out.println();

                o.notifyAll();
            }
        }
    }
}

结果:

启动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…_第2张图片

 

你可能感兴趣的:(多线程编程)