多线程交替打印的四种方法

多线程交替打印的四种方法

方法一: Wait/Notify

public class Print {
    boolean currentA = false;
    public synchronized void printA() {
        if (!currentA) {
            try {
                System.out.println("wait here");
                this.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        currentA = false;
        System.out.println("current Thread A:" + Thread.currentThread());
        this.notify();
    }

    public synchronized void printB() {
        if (currentA) {
            try {
                System.out.println("wait b here");
                this.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        currentA = true;
        System.out.println("current Thread B:" + Thread.currentThread());
        this.notify();
    }

    public static void main(String[] args) {
        Print p = new Print();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    p.printA();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    p.printB();
                }
            }
        }).start();
    }

}

​ 这里利用的是synchronized持有对象锁的特点,对于同样一个实例对象,同时只有一个线程可以进入对象内部执行方法,这样变相实现互斥的功能。但是利用的是重量级锁,也就是同步锁,虽然有锁升级的过程,但是性能还是比较低。下面用volatile 进行优化

方法二:自旋+volatile

**
 * @program: test
 * @description:
 * @author: Mr.Wang
 * @create: 2022-01-11 15:24
 **/
public class Print {
    volatile int num  = 0;
    public void printA() {
        while (num % 2 ==0) {
//            Thread.yield();
        }
        num++;
        System.out.println("current Thread A:" + Thread.currentThread()+num);
    }

    public void printB() {
        if (num%2 !=0) {
            try {
//                Thread.yield();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        num++;
        System.out.println("current Thread B:" + Thread.currentThread()+num);
    }

    public static void main(String[] args) {
        Print p = new Print();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    p.printA();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    p.printB();
                }
            }
        }).start();
    }
}

利用了volitile的内存可见性的特点,但是使用了自旋,比较消耗CPU资源

方法三:lock

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: test
 * @description:
 * @author: Mr.Wang
 * @create: 2022-01-11 15:24
 **/
public class Print {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    Lock lock = new ReentrantLock(true);
    volatile int num  = 0;
    public void printA() {
        lock.lock();
        try {
            num++;
            System.out.println("current Thread A:" + Thread.currentThread()+num);
        }catch (Exception e){

        }finally {
            lock.unlock();
        }
    }
    public void count(){
        countDownLatch.countDown();
    }

    public void printB() {
        lock.lock();
        try {
            num++;
            System.out.println("current Thread B:" + Thread.currentThread()+num);
        }catch (Exception e){

        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        Print p = new Print();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    p.countDownLatch.await();
                }catch (Exception e){
                    e.printStackTrace();
                }
                for (int i = 0; i < 50; i++) {
                    p.printA();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                p.count();
                for (int i = 0; i < 50; i++) {
                    p.printB();
                }
            }
        }).start();
    }

注意这里我试用了公平锁,避免同一个线程多次获取锁的情况来实现顺序打印,同时使用

countDownLatch让两个线程同时开始

方法四: Condition

public class Print {
    private static Lock lock = new ReentrantLock();
    private static Condition A = lock.newCondition();
    private static Condition B = lock.newCondition();
    private static Condition C = lock.newCondition();

    private volatile   static int count = 0;
    private  static int sum=1;

    static class ThreadA extends Thread {
        @Override
        public void run() {
            try {
                lock.lock();

                for (int i = 0; i < 10; i++) {
                    if (count % 3 != 0){//注意这里是不等于0,也就是说没轮到该线程执行,之前一直等待状态
                        A.await(); //该线程A将会释放lock锁,构造成节点加入等待队列并进入等待状态
                    }
                    System.out.println("-------第"+sum+"次--------");
                    System.out.println("A");
                    count++;
                    B.signal(); // A执行完唤醒B线程
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    static class ThreadB extends Thread {
        @Override
        public void run() {
            try {
                lock.lock();
                for (int i = 0; i < 10; i++) {
                    if (count % 3 != 1)
                        B.await();// B释放lock锁,当前面A线程执行后会通过B.signal()唤醒该线程
                    System.out.println("B");
                    count++;
                    C.signal();// B执行完唤醒C线程
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    static class ThreadC extends Thread {
        @Override
        public void run() {
            try {
                lock.lock();
                for (int i = 0; i < 10; i++) {
                    if (count % 3 != 2)
                        C.await();// C释放lock锁
                    System.out.println("C");
                    count++;
                    sum++;
                    A.signal();// C执行完唤醒A线程
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new ThreadA().start();
        new ThreadB().start();
        new ThreadC().start();
    }
}

你可能感兴趣的:(java,java,开发语言,后端)