作者:折纸
集体博客:https://www.zhezhi.press
记录菜鸡的成长之旅!
LockSupport
原理
贴一个之前的笔记,字迹比拟潦草zz 有空的时候这里会补上阐明
实现1
import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.LockSupport;class Main{ static Thread a = null, b=null; public static void main(String[] args){ a = new Thread(new Runnable() { @Override public void run() { for(int i = 1;i<=99;i+=2){ System.out.println(i); LockSupport.unpark(b); LockSupport.park(); } } }); b = new Thread(new Runnable() { @Override public void run() { for(int i = 2;i<=100;i+=2){ LockSupport.park(); System.out.println(i); LockSupport.unpark(a); } } }); a.start(); b.start(); }}
实现2
import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.locks.LockSupport;public class Main { static AtomicInteger num = new AtomicInteger(1); public static void main(String[] args) throws InterruptedException { Print p1 = new Print(); Print p2 = new Print(); Thread t1 = new Thread(p1); //t1对应p1工作 但p1工作的线程挂在t2上不便t1唤醒它 Thread t2 = new Thread(p2); t1.setName("thread-cc-1"); t2.setName("thread-cc-2"); p1.setT(t2);//给p1的Thread设置为t2不便t1唤醒它 p2.setT(t1); t1.start(); t2.start(); // 唤醒线程t1打印奇数,线程1打印奇数,线程2打印偶数 LockSupport.unpark(t1); } static class Print implements Runnable { private volatile Thread t; @Override public void run() { while (true) { // 进入之后立刻阻塞 LockSupport.park(); if (num.get() > 100) { LockSupport.unpark(t); return; } System.out.println(Thread.currentThread().getName() + " : " + num.getAndIncrement()); // 这里的t实际上是与currentThread不同的另一个线程 // 因而实现了 奇数唤醒偶数线程,偶数唤醒奇数线程 LockSupport.unpark(t); } } public void setT(Thread t) { this.t = t; } }}
点评:可拓展性实现2绝对较差一些(实现3个线程交替打印etc)
Synchronized + wait/notify实现
public class Main{ public static void main(String[] args){ MyPrint print = new MyPrint(); Thread thread1 = new Thread(print,"A"); Thread thread2 = new Thread(print,"B"); thread1.start(); thread2.start(); }}class MyPrint implements Runnable{ int i = 1; @Override public void run() { while (true){ synchronized (this){ this.notify();//在这里唤醒的目标是为保障拿到锁的线程只有一个 // 不会立刻开释锁 退出代码块才会开释锁 if (i <= 100){ System.out.println(Thread.currentThread().getName()+" "+i++); }else { return; } try { this.wait();//打印过数据的线程期待 必须等到没打印过数字的拿到锁了能力唤醒 } catch (InterruptedException e) { e.printStackTrace(); } } } }}