Skip to content

Latest commit

 

History

History
116 lines (99 loc) · 3.13 KB

多线程.md

File metadata and controls

116 lines (99 loc) · 3.13 KB

Java

两个交替打印数字

package src.MultiThreading.AlternatePrint;


public class AP1 {

    private static int num = 0;

    public static void main(String[] args) {
        Object lock = new Object();
        Thread thread1 = new Thread(() -> {
            while(true){
                synchronized (lock){
                    lock.notify();
                    System.out.println("THREAD1 " + num++);
                    try {
                        Thread.sleep(1000);
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        Thread thread2 = new Thread(() -> {
            while(true){
                synchronized (lock){
                    lock.notify();
                    System.out.println("THREAD2 " + num++);
                    try {
                        Thread.sleep(1000);
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        thread1.start();
        thread2.start();
    }
}

三个线程交替打印

package src.MultiThreading.AlternatePrint;

class ThreeThreadAP2 {


    private static int num = 0;

    private static Object lock = new Object();


    public static void main(String[] args) {

        new Thread(() -> {
            while(true){
                synchronized (lock){
                    if(num % 3 == 0){
                        lock.notifyAll();
                        System.out.println("Thread1 " + num++);
                        try {
                            Thread.sleep(1000);
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                }
            }
        }).start();
        new Thread(() -> {
            while(true){
                synchronized (lock){
                    if(num % 3 == 1){
                        lock.notifyAll();
                        System.out.println("Thread2 " + num++);
                        try {
                            Thread.sleep(1000);
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }).start();
        new Thread(() -> {
            while(true){
                synchronized (lock){
                    if(num % 3 == 2){
                        lock.notifyAll();
                        System.out.println("Thread3 " + num++);
                        try {
                            Thread.sleep(1000);
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }).start();
    }
}