欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多线程 -- 锁 lock condition

程序员文章站 2022-06-02 08:21:59
...

lock锁的等待与唤醒

 

*******************************************

相关接口

 

public interface Condition {

    void await() throws InterruptedException;  //等待,响应中断

    void awaitUninterruptibly();               //等待,不响应中断

    long awaitNanos(long nanosTimeout) throws InterruptedException;
                                               //限时等待

    boolean await(long time, TimeUnit unit) throws InterruptedException;
                                               //限时等待

    boolean awaitUntil(Date deadline) throws InterruptedException;
                                               //等待到某一时间

    void signal();     //唤醒一个线程

    void signalAll();  //唤醒所有线程

}

 


使用示例:

 

class Test9{

    private ReentrantLock lock=new ReentrantLock();
    private Condition condition1=lock.newCondition();
    private Condition condition2=lock.newCondition();

    public void test(){
        try{
            lock.lock();
            System.out.println("condition1 获得锁:"+System.currentTimeMillis());
            Thread.sleep(1000);
            System.out.println("condition1 即将进入等待"+System.currentTimeMillis());
            System.out.println();
            condition1.await();
            System.out.println("condition1 再次获得锁:"+System.currentTimeMillis());
            Thread.sleep(2000);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("condition1 释放锁:"+System.currentTimeMillis());
            System.out.println();
            lock.unlock();
        }
    }

    public void test2(){
        try{
            lock.lock();
            System.out.println("condition2 获得锁:"+System.currentTimeMillis());
            Thread.sleep(1000);
            System.out.println("condition2 即将进入等待"+System.currentTimeMillis());
            System.out.println();
            Thread.sleep(2000);
            condition2.await();
            System.out.println("condition2 再次获得锁:"+System.currentTimeMillis());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("condition2 释放锁:"+System.currentTimeMillis());
            System.out.println();
            lock.unlock();
        }
    }

    public void test3(){
        try{
            lock.lock();
            System.out.println("唤醒condition1获得锁:"+System.currentTimeMillis());
            condition1.signal();
            System.out.println("已经唤醒condition1");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("唤醒condition1 释放锁:"+System.currentTimeMillis());
            System.out.println();
            lock.unlock();
        }
    }

    public void test4(){
        try {
            lock.lock();
            System.out.println("唤醒condition2 获得锁:"+System.currentTimeMillis());
            condition2.signal();
            System.out.println("已经唤醒condition2");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("唤醒condition2 释放锁"+System.currentTimeMillis());
            System.out.println();
            lock.unlock();
        }
    }
}

class Thread9 implements Runnable{

    private Test9 test9;
    private int flag;

    Thread9(Test9 test9,int flag){
        this.test9=test9;
        this.flag=flag;
    }

    @Override
    public void run() {
        switch (flag){
            case 1: test9.test(); break;
            case 2: test9.test2(); break;
            case 3: test9.test3(); break;
            case 4: test9.test4(); break;
        }
    }
}

public class ThreadTest9 {

    public static void main(String[] args){
        Test9 t=new Test9();
        Thread9 t1=new Thread9(t,1);
        Thread9 t2=new Thread9(t,2);
        Thread9 t3=new Thread9(t,3);
        Thread9 t4=new Thread9(t,4);

        Thread thread1=new Thread(t1);
        Thread thread2=new Thread(t2);
        Thread thread3=new Thread(t3);
        Thread thread4=new Thread(t4);

        thread1.start();
        thread2.start();

        try{
            Thread.sleep(1000);
        }catch (Exception e){
            e.printStackTrace();
        }

        thread3.start();
        thread4.start();
    }
}

 

 

************************************************************

控制台输出:

 

 

condition1 获得锁:1568856711825
condition1 即将进入等待1568856712840

condition2 获得锁:1568856712841
condition2 即将进入等待1568856713842

唤醒condition1获得锁:1568856715848
已经唤醒condition1
唤醒condition1 释放锁:1568856715849

唤醒condition2 获得锁:1568856715851
已经唤醒condition2
唤醒condition2 释放锁1568856715853

condition1 再次获得锁:1568856715854
condition1 释放锁:1568856717857

condition2 再次获得锁:1568856717860
condition2 释放锁:1568856717861

 

相关标签: