Summary of java threads (1) - the difference between Thread and Runnable

The difference between a thread and a process

  • Process : A process is the basic unit of resource allocation. A computer divides a certain resource, such as a certain area in memory, to a certain process. This process exclusively occupies this resource and cannot be accessed by other processes.
  • Thread : Thread is the basic unit of CPU scheduling. CPU executes a thread in a fixed time slice through the time slice rotation method. There can be one or more threads in a process, which represent different execution routes of the program, and different threads in the same process share the same memory resource.

2. How to start a thread

2.1 Thread/Runnable

2.1.1 Thread

new Thread() {
    @Override
    public void run() {
        log("Thread run");
    }
}.start();
复制代码

output:

Thread run
复制代码

2.1.2 Via Runnable

new Thread(new Runnable() {
    @Override
    public void run() {
        log("Runnable run");
    }
}).start();
复制代码

output:

Runnable run
复制代码

2.1.3 The difference between Thread and Runnable

First look at the structure of the Runnable interface

public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}
复制代码

You can see that its structure is extremely simple, with only one abstract method run().
Look again at the start() method of Thread

/**
 * Causes this thread to begin execution; 
 * the Java Virtual Machine calls the run method of this thread.
 * ...
 */
public synchronized void start() {
    ...
}
复制代码

The content of the start() method is not very important to us. The important thing is the first comment, to start the thread, the run() method will be called by the java virtual machine after the thread starts to execute, so let's take a look at the run() of the Thread. )method

@Override
public void run() {
    if (target != null) {
        target.run();
    }
}
复制代码

The run method of Thread only performs a null judgment. If the target is not empty, the run method of the target is called, then what is the target.

public Thread(Runnable target) {
    init(null, target, "Thread-" + nextThreadNum(), 0);
}
复制代码

You can see that this target is the Runnable we passed in in the Thread constructor, which is how we started the thread in Section 2.1.2.

Summarize

  1. Runnable interface is just a normal interface with abstract method run()
  2. The run method of Thread will be called by the virtual machine at a certain time after the thread calls start. If the target variable of type Runnable held in Thread is not empty, the run method of the target will be called.

think

Can we both override the run method of Thread and use runnable? It's theoretically possible, let's try it

public static void testThreadRunnable() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            log("Runnable run");
        }
    }) {
        @Override
        public void run() {
            super.run();
            log("Thread run");
        }
    }.start();
}
复制代码

output

Runnable run
Thread run
复制代码

可以看到实际上也是可以的,需要注意的是,我们重写的Thread的run方法需要调用 “super.run()”,只有这样Runnable里的run方法才会被调用。

Guess you like

Origin juejin.im/post/7087480615053819940