The Java thread class is also an object class, and its instances are inherited from java.lang.Thread or its subclass. You can create a thread in Java in the following way. Executing the thread can call the start() method of the thread:
Tread thread = new Thread();
thread.start();
In the example above, we did not write running code for the thread, so the thread terminates after calling the method.
There are two ways to write code executed when a thread runs: one is to create an instance of the Thread subclass and override the run method, and the second is to implement the Runnable interface when creating the class. Next we will explain these two methods in detail:
Create a subclass of Thread
Create an instance of the Thread subclass and override the run method. The run method will be executed after calling the start() method. The example of the above Thread subclass can be created and run in the following way:
public class MyThread extends Thread { public void run(){ System.out.println("MyThread running"); }}MyThread myThread = new MyThread(); myTread.start();Once the thread starts, the start method will return immediately, and will not wait until the run method is executed before returning. It's like the run method is executed on another CPU. When the run method is executed, the string MyThread running will be printed.
You can also create an anonymous subclass of Thread as follows:
Thread thread = new Thread(){ public void run(){ System.out.println("Thread Running"); }};thread.start();When the run method of the new thread 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 instance of the class that implements the java.lang.Runnable interface, and the methods in the instance can be called by the thread. The following is an example:
public class MyRunnable implements Runnable { public void run(){ System.out.println("MyRunnable running"); }}In order for threads to execute the run() method, it is necessary to pass the instance object of MyRunnable into the constructor of the Thread class. Examples are as follows:
Thread thread = new Thread(new MyRunnable());
thread.start();
When the thread runs, it will call the run method that implements the Runnable interface. "MyRunnable running" will be printed in the above example.
Similarly, you can create an anonymous class that implements the Runnable interface, as shown below:
Runnable myRunnable = new Runnable(){ public void run(){ System.out.println("Runnable running"); }}Thread thread = new Thread(myRunnable);thread.start();Create a subclass or implement a Runnable interface?
There is no definite answer to which of these two methods is better, and they can meet the requirements. In my personal opinion, I prefer to implement the Runnable interface method. Because the thread pool can effectively manage threads that implement the Runnable interface, if the thread pool is full, new threads will queue up for execution until the thread pool is idle. And if the thread is implemented by implementing the Thread subclass, this will be a little more complicated.
Sometimes we need to integrate the two ways of implementing Runnable interface and Thread subclass at the same time. For example, an instance that implements a Thread subclass can execute multiple threads that implement the Runnable interface. A typical application is thread pool.
Common error: Calling the run() method instead of the start() method
A common mistake made by creating and running a thread is to call the thread's run() method instead of the start() method, as shown below:
Thread newThread = new Thread(MyRunnable());
newThread.run(); // should be start();
At first you don't feel anything wrong, because the run() method is indeed called as you wish. However, in fact, the run() method is not executed by the new thread that just created, but is executed by the current thread that created the new thread. That is, it is executed by the thread that executes the above two lines of code. To make the created new thread execute the run() method, the new thread's start method must be called.
Thread name
When creating a thread, you can give the thread a name. It helps us distinguish different threads. For example: If multiple threads are written to System.out, we can easily find out which thread is outputting through the thread name. Examples are as follows:
MyRunnable runnable = new MyRunnable();Thread thread = new Thread(runnable, "New Thread");thread.start();System.out.println(thread.getName());
It should be noted that because MyRunnable is not a subclass of Thread, the MyRunnable class does not have a getName() method. You can get a reference to the current thread in the following ways:
Thread.currentThread();
String threadName = Thread.currentThread().getName();
Example thread code:
Here is a small example. First, output the thread name of the main() method. This thread is allocated by the JVM. Then open 10 threads and name them 1~10. Each thread exits after outputting its own name.
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(); } }}It should be noted that although the order of starting threads is ordered, the order of execution is not ordered. That is to say, 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 and the operating system determine the execution order of threads, and the startup order of it is not necessarily consistent.
The above is a compilation of the information created by Java threads. We will continue to add relevant information in the future. Thank you for your support for this website!