Multithreading (1) - Introduction to Java Multithreading

First, the implementation of the thread

 

There are two ways to implement threads: inherit the Thread class and implement the Runnable interface

1. Inherit the Thread class
public class MyThread extends Thread{

    @Override
    public void run() {
        System.out.println("Mythread执行");
    }

}
public class TestMain {
    public static void main(String[] args) {
        MyThread mythread = new MyThread();
        mythread.start();
    }
}
Key points: Inherit the Thread class, rewrite the run method, and write code in the run method to implement the business. Multithreading is started by the start method of the Thread instance.
 
2. Implement the Runnable interface
public class MyRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println("MyRunnable执行");
    }
}
public class TestMain {
    public static void main(String[] args) {
        Thread thread = new Thread(new MyRunnable());
        thread.start();
    }
}
Key points: Implement the Runnable interface, implement the run method, and write code in the run method to implement the business. Construct Thread through the Thread (new Runnable implementation class) constructor, and start multi-threading through the start method of the Thread instance.
 
 Second, the difference between Thread and Runnable

Runnable is an interface;

Thread is a class, and Thread itself is a class that implements the Runnable interface;

We know that "a class can only have one parent class, but it can implement multiple interfaces", so Runnable has better extensibility.

In addition, Runnable can also be used for "sharing of resources". That is, multiple threads are established based on a certain Runnable object, and they will share the resources on the Runnable object.

In general, it is recommended to implement multithreading via "Runnable"!

 

 

3. Why is the thread started by start instead of the run method?

 

View the start method in the Thread source code

 

public synchronized void start() {
	// If the thread is not "ready", throw an exception!
	if (threadStatus != 0)
		throw new IllegalThreadStateException();

	//Add thread to ThreadGroup
	group.add(this);

	boolean started = false;
	try {
		// Start the thread with start0()
		start0();
		 // set the started flag
		started = true;
	} finally {
		try {
			if (!started) {
				group.threadStartFailed(this);
			}
		} catch (Throwable ignore) {	
		}
	}
}

 When Thread calls the start method, it will start the start0() method, and start0() is a native method. At this time, the java virtual machine will call the run method to start another thread.

 

The current start method continues to execute. At this time, there are two threads running at the same time. When the start() method is completed, the thread start method ends, and the thread cannot be restarted.

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

 

Continue to look at the run method. If the thread itself is constructed by implementing the Runnable interface, then call the run method of Runnable, otherwise call the method of the Thread class that overrides the run method.

Therefore, the thread can only be started by the start method, and when the run method is called, the run method is just called, and another thread will not be started.

 

 

4. Summary

Once the problems that need to be solved by multi-threading are involved, there are generally shared resources, so try to implement the Runnable interface as much as possible to share resources among multi-threads.

 

 

Guess you like

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