借用网上一张图。我们知道AQS中对于抢占同步资源失败的线程,会加入到同步
队列中,这个同步队列是个双向链表,head表示获取到同步资源的线程。而下面
的condition1和condition2等成为条件队列,condition通过锁lock获得,当调用condition的await方法后,会将当前线程释放,并将当前线程加入到条件队列中,
当condition调用singal方法后,会将当前条件队列中的首节点线程加入同步队列
抢占同步资源,当condition调用singalAll方法后,会将当前条件队列中的所有节
点线程加入到同步队列抢占同步资源。
总结:其实就是线程在同步队列和条件队列之间来回倒转,只能在其中一个队列
中呆着。
题目:使用condition交替打印123,循环10次
public class ConditionTest {
static ReentrantLock lock = new ReentrantLock();
static Condition condition1 = lock.newCondition();
static Condition condition2 = lock.newCondition();
static Condition condition3 = lock.newCondition();
static volatile int flag = 1;
public static void print1(){
lock.lock();
try {
while (flag==1){
flag = 2;
System.out.println("1..."+Thread.currentThread().getName());
condition1.await();
condition2.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
private static void print2(){
lock.lock();
try {
while (flag==2){
flag=3;
System.out.println("2..."+Thread.currentThread().getName());
condition2.await();
condition3.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
private static void print3(){
lock.lock();
try {
while (flag==3){
flag=1;
System.out.println("3..."+Thread.currentThread().getName());
System.out.println("-------------");
condition3.await();
condition1.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public static void main(String[] args) {
for(int i=1;i<=5;i++){
Thread t1 = new Thread(()->{
print1();
},"name:"+i+" "+1);
Thread t2 = new Thread(()->{
print2();
},"name:"+i+" "+2);
Thread t3 = new Thread(()->{
print3();
},"name:"+i+" "+3);
t1.start();
t2.start();
t3.start();
}
}
}
题目:实现生产者消费者模型
public class ConditionTest {
private int storage;
private int putCounter;
private int getCounter;
private Lock lock = new ReentrantLock();
private Condition putCondition = lock.newCondition();
private Condition getCondition = lock.newCondition();
public void put() throws InterruptedException {
try {
lock.lock();
if (storage > 0) {
System.out.println("put wait");
putCondition.await();
System.out.println("put wait end ...");
}
storage++;
System.out.println("put => " + ++putCounter );
getCondition.signal();
} finally {
lock.unlock();
}
}
public void get() throws InterruptedException {
try {
lock.lock();
if (storage <= 0) {
System.out.println("get wait...");
getCondition.await();
System.out.println("get wait end ...");
}
storage--;
System.out.println("get => " + ++getCounter);
putCondition.signal();
} finally {
lock.unlock();
}
}
public class PutThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 2; i++) {
try {
put();
} catch (InterruptedException e) {
}
}
}
}
public class GetThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 2; i++) {
try {
get();
} catch (InterruptedException e) {
}
}
}
}
public static void main(String[] args) {
final ConditionTest test = new ConditionTest();
Thread put = test.new PutThread();
Thread get = test.new GetThread();
put.start();
get.start();
}
}
运行结果控制台打印为:
put => 1
put wait
get => 1
get wait...
put wait end ...
put => 2
get wait end ...
get => 2