论坛首页 编程语言技术论坛

多线程之ReentrantLock学习

浏览 953 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-12-15  
<pre name="code" class="java">
package com.test;

public interface IBuffer { 
    public void write(); 
    public void read() throws InterruptedException; 


</pre>

<pre name="code" class="java">
package com.test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class BufferInterruptibly implements IBuffer { 
 
//ReentrantLock :可在一个方法中获取  在令一个方法中释放的锁
    private Lock lock = new ReentrantLock(); 
 
    public void write() { 
        lock.lock(); 
        try { 
            long startTime = System.currentTimeMillis(); 
            System.out.println("开始往这个buff写入数据…"); 
            for (;;)// 模拟要处理很长时间 
            { 
                if (System.currentTimeMillis() - startTime &gt; Integer.MAX_VALUE) 
                    break; 
            } 
            System.out.println("终于写完了"); 
        } finally { 
            lock.unlock(); 
        } 
    } 
 
    public void read() throws InterruptedException{ 
    /**
    * lockInterruptibly()方法,说明该线程调用该read方法时可以被中断,不比等待write方法结束 释放锁
    * lock()方法 说明 read方法 和write 保持同一把锁 ,只有write方法释放该锁 read方法才可操作 否则持续登录 直到获取该锁
    */
        lock.lockInterruptibly();// 注意这里,可以响应中断 
        //lock.lock() ;
        try { 
            System.out.println("从这个buff读数据"); 
        } finally { 
            lock.unlock(); 
        } 
    } 
 
}

</pre>

<pre name="code" class="java">
package com.test;

public class Test { 
   public static void main(String[] args) { 
       IBuffer buff = null; 
     
       buff = new BufferInterruptibly();    
       final Writer writer = new Writer(buff); 
       final Reader reader = new Reader(buff); 
       writer.start(); 
   try {
Thread.sleep(2000) ;
} catch (InterruptedException e) {
e.printStackTrace();
}
       reader.start(); 
       new Thread(new Runnable() { 
           public void run() { 
               long start = System.currentTimeMillis(); 
               for (;;) { 
                   // 等5秒钟去中断读 
                   if (System.currentTimeMillis() - start &gt; 5000) { 
                       System.out.println("不等了,尝试中断"); 
                       reader.interrupt(); 
                       break; 
                   } 

               } 
               System.out.println("检测终端线程结束");

           } 
       }).start(); 
   }     


   class Writer extends Thread {    
       private IBuffer buff; 
    
       public Writer(IBuffer buff) { 
           this.buff = buff; 
       } 
    
       @Override 
       public void run() { 
       System.out.println("write线程开始。。。");
           buff.write(); 
           System.out.println("write线程结束!");
       } 
   } 
    
   class Reader extends Thread { 
       private IBuffer buff; 
       public Reader(IBuffer buff) { 
           this.buff = buff; 
       } 
       @Override 
       public void run() { 
           try { 
           System.out.println("read线程开始...");
               buff.read(); 
           } catch (InterruptedException e) { 
               System.out.println("我不读了");    
           } 
           System.out.println("read结束!"); 
       } 
   } 

</pre>

运行结果:
write线程开始。。。
开始往这个buff写入数据…
read线程开始...
不等了,尝试中断
我不读了
read结束!
检测终端线程结束
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics