java多线程 更优雅的实现线程同步:交替打印A、B LockSupport实现

一 问题概述

线程或者进程之间有两种关系 同步和互斥,我们通常实现同步方法是使用线程的等待唤醒机制,而等待唤醒机制的使用是建立在互斥的继承上的。但是同步线程并不一定是必须要实现互斥的。比如一个线程打印A,一个线程打印B。这两个线程就没有互斥关系,但是提出这么个需求:交替打印A、B 。我们一般的解决方案,往往要使用wait()/notify机制。

 

 

二 LockSupport 介绍

LockSupport作为一个工具类,主要学习它的方法。

park():在线程内调用,表示当前线程自我阻塞,直到获得许可证

park(线程变量):让指定的线程获得许可证。

一看这两个方法的定义,显然可以利用这两个方法实现线程的顺序调用(同步)

 

三 两种思路实现交替打印A/B

等待唤醒机制:

/**
 * @program: test
 * @description: 交替打印A/B 等待唤醒机制
 * @author:
 * @create: 2019-07-22 14:28
 */
public class Test3 {
   static  class MyRun implements Runnable {
        static int i = 0;
        @Override
        public synchronized  void run() {
            for (int j = 0; j < 10; j++) {
                if(i%2==0)
                    System.out.println(Thread.currentThread().getName()+":A");
                else
                    System.out.println(Thread.currentThread().getName()+":B");
                i++;
                this.notifyAll();
                try {
                    if(i>=19)
                        Thread.sleep(10);
                    else
                        this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        MyRun myRun = new MyRun();
        Thread a = new Thread(myRun);
        Thread b = new Thread(myRun);
        a.start();
        b.start();
    }
}

  

LockSupport 实现

/**
 * @program: test
 * @description:交替打印A,B LockSupport实现
 * @author:
 * @create: 2019-07-22 14:03
 */
public class Test2 {
     static Thread a=null;
    static  Thread b=null;
    public static void main(String[] args) {
        a= new Thread(new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i < 10; i++) {
                    LockSupport.park();
                    System.out.println(Thread.currentThread().getName()+":B");
                    LockSupport.unpark(b);
                }
            }
        });
       b=new Thread((new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println(Thread.currentThread().getName()+":A");
                    LockSupport.unpark(a);
                    LockSupport.park();
                }
            }
        }));
        a.start();
        b.start();

    }
}

  

posted @ 2019-07-22 14:50  菜鸡wdq  阅读(1998)  评论(0编辑  收藏  举报