线程通信
Posted 骑驴晒太阳
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程通信相关的知识,希望对你有一定的参考价值。
目前了解到的有三种情况
1.condition(await/signal)
2.object(wait/notify)
3.volatile
这里只对condition和object进行测试
================================================================================================================================
package com.meritdata.cloud.thirdsystemintegration.service;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TestCondition
private Lock lock = new ReentrantLock();
private Condition condition1 = lock.newCondition();
private Condition condition2 = lock.newCondition();
private Condition condition3 = lock.newCondition();
private int index = 1;
public void test1()
lock.lock();
try
while (index != 1)
condition1.await();
index = 2;
System.out.println(Thread.currentThread().getName());
condition2.signal();
catch (InterruptedException e)
throw new RuntimeException(e);
finally
lock.unlock();
public void test2()
lock.lock();
try
while (index != 2)
condition2.await();
index = 3;
System.out.println(Thread.currentThread().getName());
condition3.signal();
catch (Exception e)
throw new RuntimeException(e);
finally
lock.unlock();
public void test3()
lock.lock();
try
while (index != 3)
condition3.await();
index = 1;
System.out.println(Thread.currentThread().getName());
condition1.signal();
catch (InterruptedException e)
throw new RuntimeException(e);
finally
lock.unlock();
public static void main(String[] args)
TestCondition test = new TestCondition();
Thread thread1 = new Thread(() ->
for (int i = 0; i < 3; i++)
test.test1();
);
Thread thread2 = new Thread(() ->
for (int i = 0; i < 3; i++)
test.test2();
);
Thread thread3 = new Thread(() ->
for (int i = 0; i < 3; i++)
test.test3();
System.out.println("================================");
);
thread3.start();
thread2.start();
thread1.start();
=============================================================================================================
package com.meritdata.cloud.thirdsystemintegration.service;
import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;
import java.util.List;
public class TestCondition
private Object lock = new Object();
private List<Integer> list = Lists.newArrayListWithCapacity(2);
public void take()
synchronized (lock)
while (CollectionUtils.isEmpty(list))
try
System.out.println(Thread.currentThread().getName()+"take wait...");
lock.wait();
catch (InterruptedException e)
throw new RuntimeException(e);
list.remove(list.get(0));
System.out.println(Thread.currentThread().getName()+"put signal...,list size:"+list.size());
lock.notify();
public void put()
synchronized (lock)
while (list.size() >= 2)
try
System.out.println(Thread.currentThread().getName()+"put wait...");
lock.wait();
catch (InterruptedException e)
throw new RuntimeException(e);
list.add(1);
System.out.println(Thread.currentThread().getName()+"take signal...,list size:"+list.size());
lock.notify();
public static void main(String[] args)
TestCondition condition = new TestCondition();
Thread thread1 = new Thread(() ->
for (int i = 0; i < 2; i++)
condition.take();
);
thread1.setName("take thread:");
Thread thread2 = new Thread(() ->
for (int i = 0; i < 3; i++)
condition.put();
);
thread2.setName("put thread:");
thread1.start();
thread2.start();
以上是关于线程通信的主要内容,如果未能解决你的问题,请参考以下文章