Condition study

并发包里面,Condition应该是很多内容的基础,主要是用到了await和signal的方法,截至贴代码吧

import org.junit.Test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTest {

    static class ShareNum {
        public int num = 1;
    }

    @Test
    public void testCondition() {
        Lock lock = new ReentrantLock();
        //条件1
        Condition condition1 = lock.newCondition();
        //条件2
        Condition condition2 = lock.newCondition();
        ShareNum shareNum = new ShareNum();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                //上锁
                lock.lock();
                try {
                    System.out.println("thread1 开始运行!");
                    while (shareNum.num <= 5) {
                        System.out.println(shareNum.num);
                        shareNum.num++;
                    }
                    System.out.println("数值到了5,可以唤醒thread2");
                    condition1.signal();
                }catch (Exception e){
                    e.printStackTrace();
                }
                finally {
                    lock.unlock();
                }
                lock.lock();
                try {
                    condition2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    System.out.println("threas1 又开始运行了!");
                    while (shareNum.num <= 15) {
                        System.out.println(shareNum.num);
                        shareNum.num++;
                    }
                } finally {
                    lock.unlock();
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //上锁
                    lock.lock();
                    while (shareNum.num <= 5) {
                        System.out.println("数值<=5 ,thread2开始等待......");
                        condition1.await();
                    }
                } catch (Exception e) {
                    System.out.println(e.toString());
                } finally {
                    lock.unlock();
                }

                try {
                    lock.lock();
                    System.out.println("thread2 开始运行");
                    while(shareNum.num  <= 10){
                        System.out.println(shareNum.num);
                        shareNum.num++;
                    }
                    System.out.println("数值到了10,可以唤醒thread1!");
                    condition2.signal();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        });
        thread2.start();
        thread1.start();
    }
}

你可能感兴趣的:(java)