Java Lock锁 Java Lock锁多线程中实现流水线任务
DescribeMe 人气:0下面程序代码通过使用Lock锁执行简单的流水线任务:
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @author lzq * @data 2020/4/29 0029 - 下午 9:48 */ public class TestLock { public static void main(String[] args) { DataSource dataSource=new DataSource(); new Thread(() -> { for (int i=0;i<10;i++){ dataSource.a(); } },"A").start(); new Thread(() -> { for (int i=0;i<10;i++){ dataSource.b(); } },"B").start(); new Thread(() -> { for (int i=0;i<10;i++){ dataSource.c(); } },"C").start(); } } class DataSource{ private int x=1; private Lock lock=new ReentrantLock(); private Condition condition1=lock.newCondition(); private Condition condition2=lock.newCondition(); private Condition condition3=lock.newCondition(); public void a(){ lock.lock(); try { while(x!=1){ condition1.await(); } System.out.println(Thread.currentThread().getName()+":aaa"); x=2; condition2.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void b(){ lock.lock(); try { while(x!=2){ condition2.await(); } System.out.println(Thread.currentThread().getName()+":bbb"); x=3; condition3.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void c(){ lock.lock(); try { while(x!=3){ condition3.await(); } System.out.println(Thread.currentThread().getName()+":ccc"); x=1; condition1.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }
执行结果如下:
对于上面代码简单分析:代码中包含简单的生产者消费者流程和Lock实现三部曲,即重复判断条件,执行逻辑,唤醒其他线程和产生锁,加锁,解锁。注意这里一点,条件判断一定要重复判断,不然可能会导致线程假醒影响结果。
因为当线程处于等待状态时,线程会释放资源,等到被唤醒的时候,从上次await的地方醒来继续执行,这时条件判断成立,执行await,其他线程再修改条件使得本线程被唤醒,此时本线程不会继续判断,而是继续执行,如果使用循环判断就能检验出条件被修改。
加载全部内容