码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA ReentrantLock的使用

时间:2015-08-11 23:01:54      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:

 

 

 

        public static void  main(String[] args){
            doSync();
        }

    private static void doSync() {

        MyThread myThread = new MyThread();

        new Thread(myThread).start();

        Thread tt = new Thread(myThread);
        tt.start();
        try {
            Thread.sleep(1000, 0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("try interuppt ");

        tt.interrupt();

    }
class MyThread implements Runnable{
    private         ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {

        try {
            System.out.println("try to wait");
            lock.lockInterruptibly();
            Thread.sleep(5000);
            //       this.wait();
            System.out.println("after wait");
            lock.unlock();


        } catch (InterruptedException e) {
            System.out.println("recv interrupt");
        }

    }
}

输出:

try to wait
try to wait
try interuppt 
recv interrupt
after wait

 

JAVA ReentrantLock的使用

标签:

原文地址:http://www.cnblogs.com/jiangz222/p/4722555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!