Interviewer: Can you tell me whether the start thread is the start() or run() method?

Candidate: Start () method

When a thread is started with start(), it enters the ready state, making the virtual processor it represents runnable, which means it can be scheduled and executed by the JVM. But that doesn’t mean the thread will run immediately. The run() method is executed only when the thread gets the time slice allocated by the CPU. Start () is the method that calls the run() method. The run() method is the one you have to override. The run() method contains the thread’s body (the real logic).

Inheriting the startup mode of Thread class

public class ThreadTest {
	public static void main(String[] args) {
		MyThread t =new MyThread();
		t.start();
	}
}
class MyThread extends Thread{
	@Override
		 public void run() {
		System.out.println("Hello World!"); }}Copy the code

Implement the startup mode of Runnable interface

public class ThreadTest {
	public static void main(String[] args) {
		Thread t =new Thread(new MyRunnable());
		t.start();
	}
}
class MyRunnable implements Runnable{
	@Override
		 public void run() {
		System.out.println("Hello World!"); }}Copy the code

In fact, the two ways of starting a thread work the same way. The first is to call a local method to start a thread, and the second is to execute the run() method of the target object in that thread. So what is the target object? To understand this, let’s look at the implementation of the Run () method of Thread:

public void run() {
	if (target != null) {
		target.run();
	}
}
Copy the code

When the new Thread(Runnable Target) constructor is called, the instance of the class that implements the Runnable interface is set to the target object of the subject to be executed by the Thread. When the Thread is started, The instance’s run() method is executed.

When we implement threads as threads, the run() method of the Thread is overridden, so that when the Thread starts, it executes the object’s own run() method.

To sum up: if we inherit the Thread class, the target is the Thread object itself, and if we implement the Runnable interface, the target is an instance of the class that implements the Runnable interface.

Let’s take a look at another question that has been floating around in the written test of major interview companies:

public class EqualsTest {
	public static void main(String args[]) {
		Thread t = new Thread() {
			public void run() { pong(); }}; t.run(); System.out.print("ping");
	}
	static void pong() {
		System.out.print("pong"); }}Copy the code

The standard answer here is: Pongping

The thread’s run method is called directly from the top down, just like a normal method, so the result is pongping. If you start with t.start(), the result will not be fixed, because there are two threads, the main thread and the T thread, the CPU time will not be fixed, and whoever gets the CPU execution first will print the result first. So the end result could be Pongping or Pingpong.

Write in the last