使用ReentrantLock


public class TestLock {

	public TestLock() {
		// TODO Auto-generated constructor stub
	}
	
	public void reentrantLock() {
		boolean useSynchronized = false;
		
		IBuffer buff = null;
              if(useSynchronized){
                buff = new Buffer();
              }else{
                buff = new BufferInterruptibly();   
              }
        final Writer writer = new Writer(buff);
        final Reader reader = new Reader(buff);
        
        writer.start();
        reader.start();
        
        new Thread(new Runnable() {
            public void run() {
                long start = System.currentTimeMillis();
                for (;;) {
                    // 等5秒钟去中断读
                    if (System.currentTimeMillis() - start > 5000) {
                        System.out.println("不等了,尝试中断");
                        reader.interrupt();
                        break;
                    }

                }

            }
        }).start();
		
	}
	

	public static void main(String[] args) {
		new TestLock().reentrantLock();

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

	
	public class Buffer implements IBuffer{
	    private Object lock;

	    public Buffer() {
	        lock = this;
	    }

	    public void write() {
	        synchronized (lock) {
	            long startTime = System.currentTimeMillis();
	            System.out.println("开始往这个buff写入数据…");
	            for (;;)// 模拟要处理很长时间
	            {
	                if (System.currentTimeMillis() - startTime > 30000)
	                    break;
	            }
	            System.out.println("终于写完了");
	        }
	    }

	    public void read() {
	        synchronized (lock) {
	            System.out.println("从这个buff读数据");
	        }
	    }
	}
	
	class BufferInterruptibly implements IBuffer{

	    private ReentrantLock lock = new ReentrantLock();

	    public void write() {
	        lock.lock();
	        try {
	            long startTime = System.currentTimeMillis();
	            System.out.println("开始往这个buff写入数据…");
	            for (;;)// 模拟要处理很长时间
	            {
	                if (System.currentTimeMillis() - startTime > 30000)
	                    break;
	            }
	            System.out.println("终于写完了");
	        } finally {
	            lock.unlock();
	        }
	    }

	    public void read() throws InterruptedException{
	        lock.lockInterruptibly();// 注意这里,可以响应中断
//	    	boolean lockFlag = lock.tryLock(5, TimeUnit.SECONDS);
	        try {
	            System.out.println("从这个buff读数据");
	        } finally {
	        	lock.unlock();
	        }
	    }

	}
	
	
	class Writer extends Thread {

	    private IBuffer buff;

	    public Writer(IBuffer buff) {
	        this.buff = buff;
	    }

	    @Override
	    public void run() {
	        buff.write();
	    }

	}
	
	
	class Reader extends Thread {

	    private IBuffer buff;

	    public Reader(IBuffer buff) {
	        this.buff = buff;
	    }

	    @Override
	    public void run() {

	        try {
	            buff.read();
	        } catch (InterruptedException e) {
	            System.out.println("我不读了");  
	        }

	        System.out.println("读结束");

	    }
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值