01_Traditional thread review

We know that there are two traditional ways to create threads:

1. Inherit the Thread class and override the run() method

// inherit Thread
class MyThread extends Thread{
	@Override
	public void run(){
		// do something
	}
}
// start the thread
MyThread t1 = new MyThread();
t1.start();

 

2. Implement the Runnable interface, override the run() method, and use the Runnalbe object to construct the Thread class

// Implement the Runnable interface
Class MyRunnable implements Runnable{
	@Override
	public void run(){
		// do something
	}
}
// start the thread
Thread t2 = new Thread(new MyRunnable);
t2.start();

The way to create a thread is very simple, but after more discussion, here are two questions:

Question 1: Can an InterruptedException be thrown in the run() method, so that when calling Thread.sleep() in the run() method, there is no need to try...catch..

Question 2: If the run() method of the Thread class that is overridden at the same time also passes a Runnable object for the Thread, will the program run the code of the run() method of the Thread class or the code of the run() method of the Runnable object?

 

answer:

Question 1, obviously not, the code can't even be compiled after writing, we look at the source code of the run() method of the Thread class, we can see,

The run() method of the Thread class does not throw an exception, and the subclass inherits the run() method of the parent class

It cannot throw exceptions. This is the knowledge of inheritance and will not be discussed in depth here for the time being.

Source code:

class MyThread extends Thread{
	@Override
	public void run() throws InterruptedException {
		Thread.sleep(1000L);
	}
}

 Compiler hints:

 

Question 2: We know that no matter which method is used to create a thread, the run() method of the Thread class will eventually be called.

Looking at the source code of the Thread class, we can see that there is the following code in the run method of the Thread class:

if (target != null) {

            target.run();

    }

And this target is the Runnalbe class we passed in in the construction method, we should understand here,

There are two cases here:

1) Case 1: If the run method of the Thread class is overridden, and the super.run() method is not called in the run method,

Then the program will only execute the run method code in the Thread class

2) Case 2: If the run method of the Thread class is overridden, and the super.run() method is called in the first line of the run method,

The program first executes the run method code in the Runnable class, and then executes the run method code in the Thread class.

Source code:

package com.sam;

/**
 * Traditional thread review
 * @author SAM
 *
 */
public class TraditionalThread {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Thread(new Runnable() {
			
			// Fun method of Runnable
			@Override
			public void run() {
				System.out.println("This is the run method of the Runnable class TName=" + Thread.currentThread().getName());				
			}
		}){
			// run method of Thread class
			@Override
			public void run() {
				//super.run();
				System.out.println("This is the run method of the Thread class TName=" + Thread.currentThread().getName());
			};
		}.start();
	}
}

 Program execution result:


 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326243271&siteId=291194637