1: The difference between inheriting thread and implementing runnable creating Threads:
Inheriting the object created by thread directly from start () is ready, but the new object using runnable must be new thread (XX) before start ()
This means that a class that implements runnable before start () is a new object that can be passed multiple times to the new thread () to create multiple threading instances and multiple start (), which means
The runnable data can be shared, but the thread created by inheriting threads does not have this capability.
classThread1 extends thread{Private intCount=5; PrivateString name; PublicThread1 (String name) { This. name=name; } Public voidrun () { for(inti =0; I <5; i++) {System. out. println (name +"Run count="+ count--); Try{sleep (int) Math.random () *Ten); } Catch(interruptedexception e) {e.printstacktrace (); } } } } Public classMain { Public Static voidMain (string[] args) {Thread1 mTh1=NewThread1 ("A"); Thread1 MTH2=NewThread1 ("B"); Mth1.start (); Mth2.start (); } }
2: The status of the thread:
|-blocking-| | | Ready -can run-in-run - dead | / | | / | lock ------Wait
Run---To run: Thread.yield ()
Run-wait: object.wait ()
Run-block: Thread.Sleep () Thread.Join ()
Run-Lock: Synchronized
Wait-Lock: object.notify () or wait time to
Lock--Can be run:
3: Parse the difference between sleep () wait () yield ():
Sleep wait yield belongs to class: thread Object thread Purpose: run to block run to wait Run to run or immediately re-run lock hold state: do not release release release after releasing CPU resources: Allow a run that is lower than its own priority to only allow execution higher than its own priority, or to perform conditions of use on its own: c16/> any place
4: Common methods
Sleep (): Forces a thread to sleep n milliseconds.
IsAlive (): Determines whether a thread is alive.
Join (): Waits for the thread to terminate.
Activecount (): The number of active threads in the program.
Enumerate (): Enumerates the threads in the program.
CurrentThread (): Gets the current thread.
Isdaemon (): Whether a thread is a daemon thread.
Setdaemon (): Sets a thread as the daemon thread. (The difference between a user thread and a daemon thread is whether to wait for the main thread to end up relying on the main thread end)
SetName (): Sets a name for the thread.
Wait (): Forces a thread to wait.
Notify (): Notifies a thread to continue running.
SetPriority (): Sets the priority of a thread.
5:synchronized
A:public synchronizedvoidMETHODAAA ()//The lock object is the object that calls this method when the static method is modified { // ....}b: Public voidMETHOD3 (Someobject so)//The lock object is an object in parentheses when the block is decorated{synchronized (so) {//... ..}}c:class foo{ PublicSynchronizedStatic voidMETHODAAA ()//the synchronous static function lock object is the current class { } Public voidmethodbbb () {synchronized (Foo.class)//equal to the above } }
6: classic Example
Public classTest { Public classMyThread implements runnable{PrivateString ThreadName; PrivateObject Pre; PrivateObject Curr; PublicmyThread (String n,object p,object c) { This. threadname=N; This. pre=p; This. curr=B; } Public voidrun () {//TODO auto-generated Method Stub intI=Ten; while(i>0{synchronized (pre) {synchronized (Curr) {System. out. println (ThreadName); I--; Curr.notify ();//notifies the next object to acquire a lock } Try{pre.wait (); }Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }//releasing locks held on the pre object } } } } Public Static voidMain (string[] args) {Object a=NewObject (); Object b=NewObject (); Object C=NewObject (); Test T=NewTest (); MyThread PA=t.NewMyThread ("A", C,a); NewThread (PA). Start (); MyThread PB=t.NewMyThread ("B", A, b); NewThread (Pb). Start (); MyThread PC=t.NewMyThread ("C", B,c); NewThread (PC). Start (); }}
Java Multithreading considerations