Thread implements the Runnable interface and implements a number of practical methods
public static native void yield ();
This method frees the CPU, but does not release the acquired lock, and other ready threads are likely to get an execution opportunity, and it is possible for it to be executed again.
public static native void sleep (long Millis) throws interruptedexception;
This method frees the CPU, but does not release the acquired lock, and the other ready thread will get an execution opportunity, and the current thread continues to execute after the sleep time has ended
Public final void Join () throws interruptedexception;
Once the call to the host thread is blocked, continue after the current thread has ended
Public final synchronized void join (long Millis) throws interruptedexception;
Once the call to the host thread is blocked, wait until the Millis time has ended
public void interrupt ();
Break Thread in
Public final void Setdaemon (Boolean on);
On=true indicates that the thread is marked as a daemon thread (such as a GC thread), and the JVM exits if all running threads are daemon threads
ImportJava.util.Map;ImportJava.util.concurrent.ConcurrentHashMap;ImportJava.util.concurrent.atomic.AtomicInteger;ImportOrg.junit.Assert;Importorg.junit.Test;/*** @Description: How to test thread*/ Public classThreadmethodstest {StaticString sleep = "Sleep"; StaticString yield = "yield"; StaticString join = "Join"; Privatemap<string, threadmethodenum> map =NewConcurrenthashmap<>(); classTheadsleepImplementsRunnable {Private intMillis; PublicTheadsleep (intMillis) { This. Millis =Millis; } @Override Public voidrun () {LongThreadId =Thread.CurrentThread (). GetId (); Threadmethodenum Sleep=Threadmethodenum.sleep; Sleep.init (THREADID); Try{thread.sleep (Millis); } Catch(interruptedexception e) {e.printstacktrace (); } sleep.stop (); Map.put (Sleep.getmethod (), sleep); } } classTheadyieldImplementsRunnable {@Override Public voidrun () {LongThreadId =Thread.CurrentThread (). GetId (); Threadmethodenum yield=Threadmethodenum.yield; Yield.init (THREADID); Thread.yield (); Yield.stop (); Map.put (Yield.getmethod (), yield); } } classTheadjoinImplementsRunnable {Privatethread thread; Private intMillis = 0; Publictheadjoin (thread thread) { This. Thread =thread; } PublicTheadjoin (thread thread,intMillis) { This. Thread =thread; This. Millis =Millis; } @Override Public voidrun () {Try { LongThreadId =Thread.CurrentThread (). GetId (); Threadmethodenum Join=Threadmethodenum.join; Join.init (THREADID); if(Millis > 0) { This. Thread.Join (Millis); } Else { This. Thread.Join (); } join.stop (); Map.put (Join.getmethod (), join); } Catch(interruptedexception e) {e.printstacktrace (); } } } FinalAtomicinteger Interruptcount =NewAtomicinteger (0); Final intInterruptindex = 100; classThreadinterruptextendsThread {@Override Public voidrun () { while(!isinterrupted ()) {Interruptcount.incrementandget (); if(Interruptcount.intvalue () = =Interruptindex) {interrupt (); } }} @Test Public voidTestsleep ()throwsinterruptedexception {intMillis = 1000; Thread Sleep=NewThread (NewTheadsleep (Millis)); Sleep.start (); Sleep.join (); Threadmethodenum tme=Map.get (SLEEP); //expected sleep time is close to MillisAssert.assertequals (1, Math.Round (Tme.getstoptimemillis ()-Tme.getstarttimemillis ()) * 1.0/Millis)); } @Test Public voidTestyield ()throwsinterruptedexception {Thread yield=NewThread (NewTheadyield ()); Yield.start (); Yield.join (); Threadmethodenum tme=Map.get (YIELD); //expected yield consumption time is almost 0Assert.assertequals (1, Math.Round (Tme.getstarttimemillis () * 1.0/Tme.getstoptimemillis ())); } @Test Public voidTestjoin ()throwsinterruptedexception {intMillis = 1000; Thread Sleep=NewThread (NewTheadsleep (Millis)); Sleep.start (); Thread Join=NewThread (NewTheadjoin (sleep)); Join.start (); Join.join (); Threadmethodenum tme=Map.get (SLEEP); //expected sleep time is close to MillisAssert.assertequals (1, Math.Round (Tme.getstoptimemillis ()-Tme.getstarttimemillis ()) * 1.0/Millis)); Tme=Map.get (JOIN); //expected join blocking time is close to MillisAssert.assertequals (1, Math.Round (Tme.getstoptimemillis ()-Tme.getstarttimemillis ()) * 1.0/Millis)); } @Test Public voidTestjoinmillis ()throwsinterruptedexception {intMillis = 500; Thread Sleep=NewThread (NewTheadsleep (Millis * 2)); Sleep.start (); Thread Join=NewThread (NewTheadjoin (Sleep, Millis)); Join.start (); Join.join (); Threadmethodenum tme=Map.get (SLEEP); //expected sleep cannot be set before the join block ends, so nullAssert.asserttrue (Tme = =NULL); Tme=Map.get (JOIN); //expected join blocking time is close to MillisAssert.assertequals (1, Math.Round (Tme.getstoptimemillis ()-Tme.getstarttimemillis ())/Millis)); } @Test Public voidTestinterrupt ()throwsinterruptedexception {threadinterrupt interrupt=NewThreadinterrupt (); Interrupt.start (); Interrupt.join (); Assert.assertequals (Interruptindex, Interruptcount.intvalue ()); }}enumthreadmethodenum {SLEEP (threadmethodstest.sleep) {@Override Public voidInitLongthreadId) { Super. Start (threadId); }}, YIELD (Threadmethodstest.yield) {@Override Public voidInitLongthreadId) { Super. Start (threadId); }}, JOIN (threadmethodstest.join) {@Override Public voidInitLongthreadId) { Super. Start (threadId); } }; PrivateString method; Private LongthreadId; Private LongStarttimemillis; Private LongStoptimemillis = 0; PrivateThreadmethodenum (String method) { This. method =method; } Public voidStop () { This. Stoptimemillis =System.currenttimemillis (); } PublicString GetMethod () {returnmethod; } Public LongGetThreadId () {returnthreadId; } Public LongGetstarttimemillis () {returnStarttimemillis; } Public LongGetstoptimemillis () {returnStoptimemillis; } Private voidStartLongthreadId) { This. ThreadId =threadId; This. Starttimemillis =System.currenttimemillis (); } Public Abstract voidInitLongthreadId);}
Java multithreaded Series two methods of--thread class