Java-multithreading creation, java-Multithreading
The Java Thread class is also an object class, and its instances all inherit from java. lang. Thread or its subclass. You can use the following method to create a thread in java. to execute this thread, you can call the start () method of the thread:
Tread thread = new Thread();thread.start();
In the above example, we didn't write the Running code for the thread, so the thread stops after this method is called.
There are two methods to compile the code executed during Thread running: one is to create an instance of the Thread subclass and override the run method, and the other is to implement the Runnable interface when creating the class. The following describes the two methods:
Create a subclass of Thread
Create an instance of the Thread subclass and override the run method. The run method will be executed after the start () method is called. The following is an example of creating and running the above Thread subclass:
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, instead of waiting until the execution of the run method is complete. It is as if the run method is executed on another cpu. After the run method is executed, the MyThread running string is printed.
You can also create an anonymous subclass of the Thread as follows:
Thread thread = new Thread(){ public void run(){ System.out.println("Thread Running"); }};thread.start();
After the run method of the new Thread is executed, the computer prints the "Thread Running" string ".
Implement the Runnable interface
The second way to write thread-based code is to create an instance of classes that implement the java. lang. Runnable interface. 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"); }}
To enable the Thread to execute the run () method, you need to input the Instance Object of MyRunnable In the constructor of the Thread class. Example:
Thread thread = new Thread(new MyRunnable());thread.start();
When the thread is running, it will call the run method that implements the Runnable interface. In the above example, "MyRunnable running" is printed ".
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 the Runnable interface?
There is no definite answer to which of the two methods can meet the requirements. In my personal opinion, I prefer to implement the Runnable interface. Because the thread pool can effectively manage the threads that implement the Runnable interface, if the thread pool is full, new threads will queue for execution until the thread pool is idle. If the Thread is implemented by implementing the Thread subclass, this will be more complicated.
Sometimes we need to integrate the Runnable interface and Thread subclass at the same time. For example, an instance that implements the Thread subclass can execute multiple threads that implement the Runnable interface. A typical application is the thread pool.
Common Errors: Call the run () method instead of the start () method
A common error occurred when creating and running a thread is calling 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 newly created thread, but 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 run the run () method on a new thread, you must call the start method of the new thread.
Thread name
When creating a thread, you can name the thread. It helps us to differentiate different threads. For example, if multiple threads write data to System. out, we can easily find out which thread is being output through the thread name. Example:
MyRunnable runnable = new MyRunnable();Thread thread = new Thread(runnable, "New Thread");thread.start();System.out.println(thread.getName());
Note that, because MyRunnable is not a subclass of Thread, the MyRunnable class does not have the getName () method. You can reference the current thread in the following ways:
Thread.currentThread();String threadName = Thread.currentThread().getName();
Example of thread code:
Here is a small example. First, output the name of the thread executing the main () method. This thread is allocated by JVM. Start 10 threads and name them 1 ~ 10. Each thread outputs its own name and then exits.
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 thread startup sequence is sequential, the execution sequence is not sequential. 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 in sequence. Together, the Jvm and the operating system determine the thread execution sequence. The thread startup sequence is not necessarily the same.