Reference book: Java multithreaded Programming core technology (Gaohong)
One, Java multithreading skills
1. Thread Start-up
-
- Process: QQ.exe
- Thread: QQ send a message by a thread processing, QQ transfer file by a thread processing
- Create threads: When using multithreading, the result of code execution is irrelevant to the order in which code is executed and the order in which it is called. When using multithreading, it is possible that the previous line of code has not finished executing the next line of code.
1 classAextendsThread {2 Public voidrun () {3 //...4 }5 }6 Public classDemo {7 Public Static voidMain (string[] args) {8A A =NewA ();9 A.start ();Ten } One}
-
-
- Implementing the Runnable Interface
1 classAImplementsrunnable{2 Public voidrun () {3 //...4 }5 }6 Public classDemo {7 Public Static voidMain (string[] args) {8A A =NewA ();9Thread T =NewThread (a);Ten T.start (); One } A}
-
- Multi-threaded access to the same resource avoids non-threading security issues (using synchronized adornments)
Public classMythreadextendsThread {Private intCount = 5; @Overridesynchronized Public voidrun () {Super. Run (); Count--; System.out.println ("By" + This. CurrentThread (). GetName () + "calculation, count=" +count); }} Public classTest { Public Static voidMain (string[] args) {Try{Mythread thread=NewMythread (); Thread a=NewThread (thread, "A"); Thread b=NewThread (thread, "B"); Thread C=NewThread (thread, "C"); Thread D=NewThread (thread, "D"); Thread e=NewThread (thread, "E"); A.start (); B.start (); C.start (); D.start (); E.start (); } Catch(Exception e) {e.printstacktrace (); } }}
Public classTest { Public Static voidMain (string[] args) {Try{thread.currentthread ();//returns information about which thread the code snippet is being called fromThread.CurrentThread (). GetName ();//gets the name of the current threadThread.CurrentThread (). isAlive ();//whether the current thread is in an active stateThread.Sleep (1000);//let the thread sleep for 1 secondsThread.CurrentThread (). GetId ();//gets the unique identity of the current threadThread.CurrentThread (). interrupt ();//stops the current thread (this method just hits a stop thread's tag, not really stops the thread, the thread continues to execute)Thread.CurrentThread (). interrupted ();//tests if the front thread has been interrupted and clears the interrupt stateThread.CurrentThread (). isinterrupted ();//test if the front thread has been interrupted, just testing, not doing other processingThread.CurrentThread (). Stop ();//deprecated method, directly stop the thread, so that some rational work can not be completed, while unlocking locked objects, does not guarantee that the data consistentThread.CurrentThread (). suspend ();//deprecated method, suspending a thread, using this method may result in exclusive access to public objects, making other threads inaccessible to public synchronization objectsThread.CurrentThread (). resume ();//deprecated method, continuation of the suspended threadThread.CurrentThread (). setpriority (10);//set Priority, 1 <= x <=;Thread.CurrentThread (). yield ();//Discard current CPU resources and give other tasks to exclusive CPU}Catch(Exception e) {e.printstacktrace (); } }}
2. How to make a thread pause
3. How to make a thread stop
4. Priority of Threads
5. Thread-Safety related issues
Java multithreaded programming