Cheer up, clean up the contents of Java concurrency. Start is a basic threading operation
Thread State switching:
New Thread:
@Test Public void Newtread () { new Thread (new Runnable () { @Override public void run () { System.out.println ("OK ..."); } ); T1.start (); }
To terminate a thread:
Thread.stop () is not recommended for use. It will release all monitor
Break thread in:
public void Thread.Interrupt ()//Break thread in
public boolean thread.isinterrupted ()//Determine if interrupted
public static Boolean thread.interrupted ()//Determines if the interrupt is interrupted and clears the current interrupt state
Public void run () {while (true) {if(Thread.CurrentThread (). isinterrupted ()) { System.out.println ("interruted!" ); Break ;} Thread.yield ();}} T1.start (); T1.interrupt ();
Sleep
public static native void sleep (long Millis) throws Interruptedexception
Public voidrun () { while(true){if(Thread.CurrentThread (). isinterrupted ()) {System.out.println ("Interruted!"); Break;}Try{Thread.Sleep (2000);} Catch(interruptedexception e) {System.out.println ("Interruted when Sleep");//sets the interrupt state, which clears the interrupt mark bit after an exception is thrownThread.CurrentThread (). interrupt ();} Thread.yield ();}}
Suspend and Continue execution:
–suspend () does not release the lock
– If the lock occurs before the resume (), the deadlock occurs
Waiting for thread to end (join) and humility (yeild)
Public final void Join () throws Interruptedexception
Public final synchronized void join (long Millis) throws Interruptedexception
The nature of Join
while (IsAlive ()) {
Wait (0);
}
When the thread finishes executing, the system calls Notifyall ();
Public classJoinmain { Public volatile Static intI=0; Public Static classAddthreadextendsthread{@Override Public voidrun () { for(i=0;i<10000000;i++);}} Public Static voidMain (string[] args)throwsinterruptedexception {addthread at=NewAddthread (); At.start (); At.join (); System.out.println (i);}}
Synchronized
– Specifies the Lock object: Locks the given object and obtains a lock on the given object before entering the synchronization code.
– Acts directly on the instance method: locks the current instance and obtains the current instance's lock before entering the synchronization code.
– Acts directly on a static method: It is equivalent to locking the current class and getting the lock of the current class before entering the synchronization code.
? Object.wait () obejct.notify ()
Java Basic Threading Operations (with code)