java Reentrant Lock

//Listing 7-1. Achieving Synchronization in Terms of Reentrant Locks
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock; public class A {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool();
final ReentrantLock lock = new ReentrantLock();
class Worker implements Runnable {
private final String name; Worker(String name) {
this.name = name;
} @Override
public void run() {
lock.lock();
try {
if (lock.isHeldByCurrentThread())
System.out.printf(
"Thread %s entered critical section.%n", name);
System.out.printf("Thread %s performing work.%n", name);
try {
Thread.sleep();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.printf("Thread %s finished working.%n", name);
} finally {
lock.unlock();
}
}
}
executor.execute(new Worker("ThdA"));
executor.execute(new Worker("ThdB"));
try {
executor.awaitTermination(, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
executor.shutdownNow();
}
}
上一篇:20个Linux系统监视工具


下一篇:display:block什么时候使用