//: 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();
}
}
}