【技能库】--LockSupport的使用(243)

来源:互联网 发布:java逆波兰式 编辑:程序博客网 时间:2024/05/22 06:05
LocSupport的使用:举例1(main方法中)

final Object object = new Object();final Thread thread1 = new Thread(new Runnable() {    @Override    public void run() {        System.out.println("1开始自旋");        LockSupport.park(object);//调试排查问题tools使用        System.out.println("1停止自旋");    }});Thread thread2 = new Thread(new Runnable() {    @Override    public void run() {        System.out.println("2通知 -->");        LockSupport.unpark(thread1);        System.out.println("2通知完毕-->");    }});thread1.start();System.out.println("主函数等待...");try {    Thread.sleep(1000l);} catch (InterruptedException e) {    e.printStackTrace();}thread2.start();

结果:
1开始自旋
2通知 -->
2通知完毕-->
1停止自旋

举例2:互斥锁(only one get lock)
import java.util.Queue;import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.atomic.AtomicBoolean;import java.util.concurrent.locks.LockSupport;/** * Created by xiayin on 2017/6/27. */class FIFOMutex {    private final AtomicBoolean locked = new AtomicBoolean(false);    private final Queue<Thread> waiters            = new ConcurrentLinkedQueue<Thread>();    public void lock() {        boolean wasInterrupted = false;        Thread current = Thread.currentThread();        waiters.add(current);        System.out.println("入队"+current.getName());        // Block while not first in queue or cannot acquire lock        while (waiters.peek() != current ||                !locked.compareAndSet(false, true)) {            System.out.println("准备park..."+current.getName());            LockSupport.park(this);            System.out.println("停止park"+current.getName());            if (Thread.interrupted()) // ignore interrupts while waiting                wasInterrupted = true;        }        waiters.remove();        System.out.println("出队列"+current.getName());        if (wasInterrupted)          // reassert interrupt status on exit            current.interrupt();    }    public void unlock() {        locked.set(false);        LockSupport.unpark(waiters.peek());    }    public static void main(String[] args) throws InterruptedException {        final FIFOMutex fifoMutex = new FIFOMutex();        final Thread thread0 = new Thread(new Runnable() {            @Override            public void run() {                fifoMutex.lock();                try {                    Thread.sleep(2000l);                } catch (InterruptedException e) {                    e.printStackTrace();                }                fifoMutex.unlock();            }        });        Thread thread1 = new Thread(new Runnable() {            @Override            public void run() {                fifoMutex.lock();                try {                    Thread.sleep(1000l);                } catch (InterruptedException e) {                    e.printStackTrace();                }                fifoMutex.unlock();            }        });        thread0.start();        thread1.start();        Thread.sleep(50000l);    }}

结果:
入队Thread-0
入队Thread-1
出队列Thread-0
准备park...Thread-1
停止parkThread-1
出队列Thread-1


Process finished with exit code 0