In multithreaded programming, the more important and more difficult operation is how to get the information in the thread. One way most people will take a more common approach is to store the results returned in a thread in a field, and then provide a fetch method to return the contents of the field to the caller of the method. such as the following Returnthreadinfo class:
package threadtest1;
/**
*
* @author shi mingxiang
*/
public class ReturnThreadInfo extends Thread {
private String str;
public ReturnThreadInfo() {
this.str = "Hello";
}
public void run(){
this.str = "Hello World!";
}
public String getThreadInfo(){
return this.str;
}
}
You can see that the class is a thread class and contains a field with an initial value of "Hello" str and a method that can return the STR value: GetThreadInfo (), and when this thread starts, STR is assigned the new value: "Hello world!". Now I want to start the Returnthreadinfo thread in another class and get the value "Hello world!" by the GetThreadInfo () method Variables and prints the output to the console. Here's a main class that implements the feature:
Package Threadtest1
/**
*
* @author shi Mingxiang
*/
public class main{
Public Main () {
}
/**
* @param args the command line arguments
*/
public static void Main (string[) a RGS) {
Returnthreadinfo returnthreadinfo = new Returnthreadinfo ();
Returnthreadinfo.start (); Create and start the Returnthreadinfo thread
System.out.println (Returnthreadinfo.getthreadinfo ()); Gets and outputs the value of the STR for the Returnthreadinfo object
}
}
These are the implementations that most people who are familiar with single-threaded programming are given under the first reaction. But the output of the class at run time is not the desired "Hello world!" Rather, "Hello", which is due to the competitive condition of the thread (because the returnthreadinfo thread and main thread have a priority of 5, so the Returnthreadinfo thread's Run () method is not running at a high probability. The main class is already running System.out.println (Returnthreadinfo.getthreadinfo ()), and "Hello" is output. The specific principle can be seen in another article: "Several myths of Java multithreading". Some people may immediately think that the priority of the Returnthreadinfo thread higher (such as the maximum of 10) can be returnthreadinfo the thread's Run () method first run, and then the main class System.out.println ( Returnthreadinfo.getthreadinfo ()) then run, so that the output knot must be the desired "Hello world!" Out. This method of tuning thread precedence can, of course, solve the problem in some way, but the principle of thread contention for CPU uptime is by no means just a cause of priority (higher-priority threads do not necessarily have to run first than priority threads, but are more likely). You don't want the returnthreadinfo thread to run 9,999 times more than main, but run it at the most critical time after main. So here are two more common ways to get thread information: