How to create and run java threads
Technical articles delivered first!
JavaThe thread class is also aobject kind, Its instances all inherit fromjava.lang.Thread or its subclasses。 This can be done in the following way withjava Create a thread in:
Execution of the thread can be done by calling the start() method of the thread:
In the above example, we didn't write running code for the thread, so the thread terminates after calling the method.
There are two ways to write code for thread runtime execution: One is to createThread An instance of the subclass and overriderun approach, The second is when the class is created Implementing the Runnable interface。 We will then explain these two methods in detail:
Create a subclass of Thread
establishThread An instance of the subclass and overriderun approach,run method will be called after thestart() method is executed after the。 Examples are as follows:
The above can be created and run in the following wayThread subcategory
Once the thread has been startedstart method will immediately return the, And will not wait untilrun method is executed before it returns。 It's like...run The method is used in a separatecpu It's the same as executing on。 properrun After the execution of the method, will print out the stringMyThread running。
You can also create one as followsThread anonymous subclasses of:
When the new thread's run method is executed, the computer will print out the string "Thread Running".
Implementing the Runnable interface
The second way to write thread execution code is to create a new thread that implements thejava.lang.Runnable Instances of the class of the interface, Methods in instances can be called by threads。 Examples are given below:
In order for a thread to executerun() approach, Needed inThread class constructor by passing in MyRunnable instance object of the。 Examples are as follows:
When the thread is running, it will call the run method that implements the Runnable interface. The above example will print out "MyRunnable running".
equivalent, It is also possible to create an implementation of theRunnable Anonymous Classes for Interfaces, as shown below:
Create subclasses or Implementing the Runnable interface?
There is no definitive answer as to which of these two is better, They all fit the bill.。 In my personal opinion, I prefer Implementing the Runnable interface this approach。 Because thread pools can efficiently manage the implementation ofRunnable Threads of the interface, If the thread pool is full, New threads will then be queued for execution, Until the thread pool is free。 And if the thread was created by implementingThread Subclasses implemented by, This is going to be a little more complicated。
Sometimes we have to fuse implementing both the Runnable interface and the Thread subclass. For example, an instance that implements the Thread subclass can execute multiple threads that implement the Runnable interface. A typical application is thread pooling.
Common errors: invokerun() method rather thanstart() approach
A common mistake made in creating and running a thread is to call the thread'srun() method rather thanstart() approach, as shown below:
At first you won't feel anything wrong, because the run() method does get called as you would expect. However, in fact, the run() method is not executed by the new thread that was just created, but by the current thread that created the new thread. That is, by the thread executing the two lines of code above. To make the new thread created execute the run() method, you must call the start method of the new thread.
thread name
When creating a thread, you can give the thread a name. It helps us to distinguish between different threads. For example, if there are multiple threads writing to System.out, we can easily figure out which thread is outputting by thread name. Examples are as follows.
Note that because MyRunnable is not a subclass of Thread, the MyRunnable class does not have a getName() method. A reference to the current thread can be obtained by.
therefore, The name of the current thread can be obtained with the following code:
Example of thread code:
Here is a small example. First output the name of the thread that executes the main() method. This thread is assigned by the JVM. Then open 10 threads, named 1 to 10. Each thread outputs its own name and then exits.
It is important to note that although the order in which threads are started is ordered, the order of execution is not. That is, thread #1 is not necessarily the first thread to output its name to the console. This is because threads are executed in parallel rather than sequentially. Jvm, together with the operating system, determines the order of thread execution; he and the threads are not necessarily started in the same order.