测试代码:
import java.util.concurrent.locks.ReentrantLock; public class ReenTest { public static ReentrantLock reentrantLock = new ReentrantLock(); public static void main(String[] args) { new Thread(){ public void run() { doJob(); } }.start(); new Thread(){ public void run() { doJob(); } }.start(); new Thread(){ public void run() { doJob(); } }.start(); new Thread(){ public void run() { doJob(); } }.start(); new Thread(){ public void run() { doJob(); } }.start(); } static long tm=0; public static void doJob() { long t=System.currentTimeMillis()/30000; reentrantLock.lock(); if(tm<t) { tm=t; System.out.println("Ttt"+Thread.currentThread().getName()); } else { System.out.println("OK"+Thread.currentThread().getName()); } reentrantLock.unlock(); } }