62,636
社区成员




static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
es.execute(new Runnable() {
@Override
public void run() {
foo("thread1");
}
});
es.execute(new Runnable() {
@Override
public void run() {
foo("thread2");
}
});
}
private static void foo(String threadName) {
try {
lock.tryLock();
System.out.println(lock.getHoldCount());
System.out.println(threadName);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
lock.unlock();
System.out.println(lock.getHoldCount());
}
}
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class ReentrantLockTest{
static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
es.execute(new Runnable() {
@Override
public void run() {
foo("thread1");
}
});
es.execute(new Runnable() {
@Override
public void run() {
foo("thread2");
}
});
}
private static void foo(String threadName) {
while(true){
if(lock.tryLock()){
try{
System.out.println(Thread.currentThread().getName()+":"+lock.getHoldCount());
System.out.println(Thread.currentThread().getName()+":"+threadName);
try {Thread.sleep(2000);} catch (InterruptedException e) { e.printStackTrace();}
break;
}finally {
lock.unlock();
System.out.println(Thread.currentThread().getName()+":"+lock.getHoldCount());
}
}
}
}
}