[Multithreading] _ notes on common thread operation methods

Source: Internet
Author: User

[Multithreading] _ notes on common thread operation methods
Objectives of this chapter:
Learn how to set and get the thread name
Understand the force running of threads
Measure the test taker's knowledge about thread sleep.
Understand the thread courtesy
Understand thread interruption operations

3.1 obtain and set the thread name.
In the Thread class, you can get the thread name through the getname () method, and set the thread name through the setname () method.
The thread name is generally set before the thread starts, but the name can also be set for the running thread. Two thread objects are allowed to have the same name, but this situation should be avoided as much as possible for clarity.
In addition, if the program does not specify a name for the thread, the system automatically assigns a name for the thread.
Instance 1:

Class mythread implement runnable {public void run () {for (INT I = 0; I <3; I ++) {system. out. println (thread. currentthread (). getname () + "run, I =" + I); // get the name of the current thread }}public class threadnamedemo {public static void main (string [] ARGs) {mythread MT1 = new mythread (); // instantiate the runnable object new thread (MT1 ). start (); New thread (MT1, "thread-"). start (); New thread (MT1, "thread-B "). start (); New thread (MT1 ). start ();}}

3.2 get current thread

Class mythread implement runnable {public void run () {for (INT I = 0; I <3; I ++) {system. out. println (thread. currentthread (). getname () + "run, I =" + I); // get the name of the current thread }}public class threadnamedemo {public static void main (string [] ARGs) {mythread MT1 = new mythread (); // instantiate the runnable object new thread (MT1, "thread-"). start (); mt1.run ();}}

Output:
Thread running, I = 0
Thread running, I = 1
Thread running, I = 2
Main run, I = 0
Main run, I = 1
Main run, I = 2

Mt1.run (); calling this statement is completed by the main method. That is to say, the main method itself is also a thread-the main thread.

Question: Since the main method is in the form of a thread, how many threads have been started during Java runtime?
Answer: at least two (main thread, GC)

3.3 determine whether the thread is started

Class mythread implements runnable {public void run () {for (INT I = 0; I <3; I ++) {system. out. println (thread. currentthread (). getname () + "run, I =" + I) ;}} public class threadalivedemo {public static void main (string [] ARGs) {mythread Mt = new mythread (); // instantiate the runnable subclass object thread t = new thread (MT, "Thread"); // instantiate the thread object system. out. println ("before the thread starts execution -->" + T. isalive (); T. start (); // start the thread system. out. println ("after the thread starts execution -->" + T. isalive (); For (INT I = 0; I <3; I ++) {system. out. println ("Main run -->" + I) ;}// the following output result is incorrect: system. out. println ("after code execution -->" + T. isalive ());}}

3.4 force running of threads

In thread operations, you can use the join () method to force a thread to run. Other threads cannot run during the force running of the thread. The thread can continue to run only after it is completed.
Forced running instance of main thread and thread:

Class mythread implements runnable {public void run () {for (INT I = 0; I <3; I ++) {system. out. println (thread. currentthread (). getname () + "run, I =" + I) ;}} public class threadjoindemo {public static void main (string [] ARGs) {mythread Mt = new mythread (); thread t = new thread (MT, "Thread"); T. start (); // start the thread for (INT I = 0; I <50; I ++) {if (I> 10) {try {T. join (); // thread forced run} catch (interruptedexception e) {e. printstacktrace () ;}} system. out. println ("main thread running -->" + I );}}}

3.5. Thread sleep
Sleep allows the thread to pause execution.

Use the thread. Sleep () method!

Class mythread implements runnable {public void run () {for (INT I = 0; I <50; I ++) {try {thread. sleep (500);} catch (interruptedexception e) {system. out. println (thread. currentthread (). getname () ;}}} public class threadsleepdemo {public static void main (string [] ARGs) {mythread Mt = new mythread (); // instantiate the runnable subclass object thread t = new thread (MT); T. start (); // start thread }}

Thread interruption of 3.6

The status of a thread that is interrupted by another thread. Use interrupt () to complete the operation.

Class mythread implements runnable {public void run () {system. out. print ("1. Enter the run () method"); try {thread. sleep (10000); // The thread sleep for 10 seconds. out. println ("2. Sleep completed");} catch (interruptedexception e) {system. out. println ("3. Sleep terminated"); return; // return call} system. out. println ("4, run () method ends normally") ;}} public class threadinterrupteddemo {public static void main (string [] ARGs) {mythread Mt = new mythread (); thread t = new thread (MT, "Thread"); T. start (); try {thread. sleep (2000); // The Master thread sleeps for 2 seconds} catch (interruptedexception e) {system. out. println ("3. Sleep terminated");} t. interrupt (); // interrupt thread execution }}

3.7 background thread

In Java, as long as a program is not executed (a thread is running), the entire Java Process will not disappear. Therefore, you can set a background thread, in this way, the background thread continues to execute even if the Java Process ends.
To implement this operation, use the setdaemo () method directly.

Class mythread implements runnable {public void run () {While (true) {system. Out. println (thread. currentthread (). getname () + "is running. ") ;}} Public class threaddaemondemo {public static void main (string [] ARGs) {mythread Mt = new mythread (); thread t = new thread (MT, "Thread"); T. setdaemon (true); // This thread runs T in the background. start (); // start thread }}

3.8. thread priority

Highest priority max_priority
Moderate priority norm_priority
Minimum priority min_priority

Class mythread implements runnable {public void run () {for (INT I = 0; I <5; I ++) {try {thread. sleep (500);} catch (interruptedexception e) {} system. out. println (thread. currentthread (). getname () + "run, I =" + I) ;}} public class threadprioritydemo {public static void main (string [] ARGs) {thread T1 = new thread (New mythread (), "thread a"); thread t2 = new thread (New mythread (), "thread B "); thread T3 = new thread (New mythread (), "thread C"); t1.setpriority (thread. max_priority); t2.setpriority (thread. norm_priority); t3.setpriority (thread. min_priority );}}

The priority of main () is norm_priority.

3.9. Thread courtesy
In thread operations, you can use the yield () method to temporarily grant the operations of one thread to other threads.

Class mythread implements runnable {public void run () {for (INT I = 0; I <5; I ++) {system. out. println (thread. currentthread (). getname () + "run, I"); if (I = 3) {system. out. println ("thread courtesy:"); thread. currentthread (). yield (); // thread courtesy }}} public class threadyielddemo {public static void main (string ARGs []) {mythread my = new mythread (); thread T1 = new thread (my, "thread a"); thread t2 = new thread (my, "thread B"); t1.start (); t2.start ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.