// Testlock. javapackage COM. test. thread; import Java. util. arraylist; import Java. util. list; import Java. util. concurrent. executionexception; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; import Java. util. concurrent. future; import Java. util. concurrent. timeunit; import Java. util. concurrent. locks. lock; import Java. util. concurrent. locks. reentrantlock; public class testlock {Private Static int CNT = 0; private final int num = CNT ++; Private Static lock = new reentrantlock (); Private testlock () {} Private Static testlock ins = NULL; public static/* synchronized */testlock getinstance () {If (INS = NULL) {// If the lock is declared here, this segment cannot be accessed by multiple threads. Code // Lock = new reentrantlock (); lock. lock (); try {If (INS = NULL) {timeunit. milliseconds. sleep (10); ins = new testlock () ;}} catch (interruptedexception e) {e. printstacktrace ();} finally {lock. unlock () ;}} return INS;} public int getnum () {return num;}/*** @ Param ARGs */public static void main (string [] ARGs) {executorservice SVS = executors. newcachedthreadpool (); List <future <testlock> lstobjs = new arraylist <future <testlock> (); For (INT I = 0; I <10; I ++) {lstobjs. add (SVS. submit (New testlockthread ();} For (Future <testlock> FTL: lstobjs) {try {testlock TL = FTL. get ();} catch (interruptedexception e) {e. printstacktrace ();} catch (executionexception e) {e. printstacktrace () ;}} SVS. shutdown () ;}// testlockthread. javapackage COM. test. thread; import Java. util. concurrent. callable; public class testlockthread implements callable <testlock >{@ overridepublic testlock call () throws exception {testlock TL = testlock. getinstance (); system. out. println ("call:" + TL. getnum (); Return TL ;}}