CLHLock的实际应用


//: concurrency/BasicThreads.java
// The most basic use of the Thread class.

public class BasicThreads {
  public static void main(String[] args) {
	Counter counter = new Counter();
	for (int i = 0; i < 10; i++) {
		Thread t = new Thread(new DThread(counter));
	    t.start();
	}
    
    System.out.println("Waiting for LiftOff");
  }
} /* Output: 
Thread[Thread-1,5,main]  wait
Thread[Thread-3,5,main]  wait
Thread[Thread-3,5,main]  wait
Thread[Thread-3,5,main]  wait
Thread[Thread-5,5,main]  wait
Thread[Thread-5,5,main]  wait
Thread[Thread-7,5,main]  wait
1  Thread[Thread-0,5,main]
Thread[Thread-2,5,main]  wait
Thread[Thread-7,5,main]  wait
Thread[Thread-7,5,main]  wait
Waiting for LiftOff
Thread[Thread-9,5,main]  wait
Thread[Thread-5,5,main]  wait
Thread[Thread-6,5,main]  wait
Thread[Thread-3,5,main]  wait
Thread[Thread-4,5,main]  wait
2  Thread[Thread-1,5,main]
5  Thread[Thread-4,5,main]
4  Thread[Thread-3,5,main]
Thread[Thread-6,5,main]  wait
Thread[Thread-5,5,main]  wait
Thread[Thread-9,5,main]  wait
Thread[Thread-7,5,main]  wait
Thread[Thread-8,5,main]  wait
3  Thread[Thread-2,5,main]
*///:~
public class DThread implements Runnable{
	private Counter counter;
	public DThread(Counter counter) {
		// TODO Auto-generated constructor stub
		this.counter = counter;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			System.out.println(counter.RLincrement()+ "  "+Thread.currentThread());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class CLHLock implements Lock {  
    private final AtomicReference tail;  
    private final ThreadLocal myPred;  
    private final ThreadLocal myNode;  
  
    public CLHLock() {  
        tail = new AtomicReference(new QNode());  
        myNode = new ThreadLocal() {  
            protected QNode initialValue() {  
                return new QNode();  
            }  
        };  
  
        myPred = new ThreadLocal();  
    }  
  
    @Override  
    public void lock() {  
        QNode node = (QNode) myNode.get();  
        node.locked = true;  
        QNode pred = (QNode) tail.getAndSet(node);  
        myPred.set(pred);  
        while (pred.locked) {  
        	System.out.println(Thread.currentThread()+"  wait");
        }  
    }  
  
    @Override  
    public void unlock() {  
        QNode node = (QNode) myNode.get();  
        node.locked = false;  
        myNode.set(myPred.get());  
    }  
  
    private static class QNode {  
        volatile boolean locked;  
    }

	@Override
	public void lockInterruptibly() throws InterruptedException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean tryLock() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean tryLock(long time, TimeUnit unit)
			throws InterruptedException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Condition newCondition() {
		// TODO Auto-generated method stub
		return null;
	}  
}  

import java.util.concurrent.locks.ReentrantLock;




/**
 * Counter
 * <p/>
 * Simple thread-safe counter using the Java monitor pattern
 *
 * @author Brian Goetz and Tim Peierls
 */
public final class Counter {
    private long value = 0;
    
    private CLHLock lock = new CLHLock();
    
    private ReentrantLock rlock = new ReentrantLock();

    public synchronized long getValue() {
        return value;
    }
    
    public synchronized long increment() {
    	if (value == Long.MAX_VALUE)
            throw new IllegalStateException("counter overflow");
        return ++value;
    }

    public  long Lincrement() {
    	lock.lock();
    	try{
    		 if (value == Long.MAX_VALUE)
    	            throw new IllegalStateException("counter overflow");
    	        return ++value;
    	}finally{
    		lock.unlock();
    	}
        
    }
    
    public  long RLincrement() {
    	rlock.lock();
    	try{
    		 if (value == Long.MAX_VALUE)
    	            throw new IllegalStateException("counter overflow");
    	        return ++value;
    	}finally{
    		rlock.unlock();
    	}
        
    }
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java码界探秘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值