Original address
Original Author: Jakob Jenkov
Translator: Whistling
Feedback Email: 274021963@qq.com
A Java thread is an object, like any other Java object. A thread is an instance of a class java.lang.Thread, or an instance of a subclass of this class.
In Java you can create threads like this:
Thread thread = new Thread ();
You can invoke the start () method of this thread object to start it:
Thread.Start ();
In this case, this thread does not execute any code. Therefore, it will stop immediately.
There are two ways you can specify the code that the thread will execute:
The first is to create a subclass of thread and override its run () method. The second approach is to have an object implement the Runnable () interface, and also to override the run () method.
thread Sub Class
The first way to specify the executed code for a thread is to create a subclass of thread and override its run () method. The run () method is executed by the thread after you call the SATRT () method. Here's an example:
public class Mythread extends thread{public
void Run () {
System.out.println ("Mythread running!");
}
To create and start the above thread, you can do this:
Mythread mythread = new Mythread ();
Mythread.start ();
The call to the start () method stops immediately after the thread starts, and waits after the run () method executes. The run () method executes as if it were invoked by a different CPU. When the run () method executes, the word "mythread running" is printed.
You can also create an anonymous thread subclass, like this:
Thread thread = new Thread () {public
void run () {
System.out.println ("Thread ruuning!");
}
};( Translator Note: In the original text there is less semicolon, should be the author of a clerical error
Thread.Start ();
This example prints the word "thread Running" when the Run () method is invoked by a new thread.
Implement Runnable interface
The second way to specify the code to execute for a thread is to create a class that enables him to implement the Java.lang.Runnable interface. This implements a Runable object that can be executed by one thread:
public class Myrunnable implements runnable{public
void Run () {
System.out.println ("myrunnable running!");
}
}
This thread executes the myrunable run () method through a myrunnable instance in the constructor of a thread. Look at the code:
Thread thread = new Thread (new myrunnable ());
Thread.Start ();
When this thread starts, he executes the run () method of the Myrunnable instance instead of her own run () method. The example above will print out "myrunnable running!" The words.
You can also create an anonymous class to implement the Runnable interface, like this:
Runnable myrunnable = new Runnable () {
@Override public
void Run () {
System.out.println ("Runnable running!");
}
};( Translator Note: In the original text there is less semicolon, should be the author of a clerical error thread
thread = new Thread (myrunnable);
Thread.Start ();
subclass or Runnable interface.
There is no rule to specify which of these two methods is better. Personally, I prefer to implement the Runnable interface and pass an instance that implements the thread instance (translator Note: as a parameter). (Translator Note: If you are difficult to understand the sentence I translated, refer to the original.) The original text is: I prefer implementing Runnable, and handing aninstance of the implementation to a Thread instance.) When a thread pool executes a runnable instance, if the line Cheng Chili no thread is idle, it is easy to queue the runnable instance. This is difficult for the thread subclass.
Sometimes you have to implement the Runnable interface as well as the thread subclass. For example, if you create a thread subclass, he can implement multiple runnable interfaces. This approach is usually used when you want to implement a thread pool.
Common pitfalls: Calling the Run () method instead of the start () method
A common mistake when creating and starting a thread is to invoke this thread's run () method instead of the start () method, like this:
Thread newthread = new Thread (new Myrunnable ());(Translator Note: Here the original author myrunnable () before the keyword new, should be a clerical error)
Newthread.run (); This should be the start () method
You may not find any problems at first, because the runable run () method is executed as you expected. However, the run () method is not performed by the thread you just created (the translator note: refers to the Newthread object). This is not the run () method that the thread executes, but rather the run () method creates the thread. In other words, the thread executes the above two lines of code, and the newly created thread invokes the run () method that executes the myrunable instance. (Translator note: May be a bit of a detour, so to understand, the first line of code creates a thread object Newthread. The second line of code creates another thread. We call it tempthread. Tempthread executes the run () method of the Myrunnable instance) for this newthread, you must call its start () method.
Thread Name
When you create a thread, you can give him a name. This name allows you to differentiate it from other threads. For example, when multiple threads are using System.out output, you can easily see which thread is outputting the text. Here's an example:
Thread thread = new Thread ("new thread") {public
void run () {
System.out.println ("Run by:" +getname ());
}
};
Thread.Start ();
System.out.println (Thread.getname ());
}
Notice that the "New Thread" string is passed as a parameter to the Thread's constructor. This string is the name of this thread. The thread's name can be obtained through the thread's GetName () method. When you are using an implementation of a runnable interface you can also pass a name to this thread. Please see:
Myrunnable runnable = new myrunnable ();
Thread thread = new Thread (runnable, "new Thread");
Thread.Start ();
System.out.println (Thread.getname ());
Note: No matter what, as long as the Myrunable class is not a subclass of thread, he does not have permission to make thread's GetName method execute. You can use the following code to get the currently executing thread:
Thread.CurrentThread ();
To get the name of the currently executing thread, you can do this:
String threaname = Thread.CurrentThread (). GetName ();
a threading Example
Here's a little example. First he prints out the name of the thread that currently executes the main () method. This thread is assigned by the JVM. Then he started 10 threads and gave them a number as the name ("+i"). Each thread then prints out its own name. Then stop execution.
public class Threadexample {public
static void main (String []args) {
System.out.println (thread.currentthread) ( ). GetName ());
for (int i=0;i<10;i++) {
new Thread (' "+i) {public
void run () {
System.out.println (" Thread: "+getname () + "Running");
}
. Start ();
Parallel}}}
Note that even though threads are started sequentially (1,2,3, etc.) they may not be executed sequentially. means that thread1 may not be the first to write his name in System.out. This is because, in principle, threads are executed in parallel rather than sequentially. The JVM and/or the operating system determine the order in which the threads are executed. This order is not necessarily the same as the order in which the threads are started.