Talk about the understanding of java thread (1) -------- thread creation and start

I recently reviewed the relevant knowledge points of the next thread, and share it with you here.

Partners who are new to Java may be confused about threads or something, and learn a little by rote in a daze. The knowledge in them always feels dizzy, as if they understand it but don't.

To understand the knowledge points of java threads, you must first understand the meaning of processes and threads. The following is an excerpted description of threads and processes.

 

A process is an execution process of a program, and is the basic unit of the system to run a program, so the process is dynamic. The system running a program is the process of a process from creation, operation to death.

A thread is similar to a process, but a thread is a smaller unit of execution than a process. A process can spawn multiple threads during its execution. Different from a process, multiple threads of the same kind share the process’s heap and method area resources, but each thread has its own program counter , virtual machine stack, and local method stack , so the system is generating a thread or between each thread. When switching between jobs, the burden is much smaller than that of the process. Because of this, the thread is also called a lightweight process.

For the virtual machine that has been used in Java, if you start a main() method, you start a JVM process, and this process includes daemon threads (garbage collection) and ordinary threads (tasks in the main method) )

When we first started contacting java threads, we know two common ways to create java threads, inheriting Thread and implementing Runnable. If you don't understand, just remember it in advance, this java language is so stipulated, there is nothing to say, and you will get used to it after a long time.

The following implementation writes two simple threads:

Inherit Thread

package com.example.demoproject;

public class ThreadEx extends Thread{


    public ThreadEx(String name) {
        super(name);
    }

    /**
     * 重写run()方法
     */
    @Override
    public void run() {
        System.out.println("运行方式"+this.getName()+"  ThreadEx方法名称:"+Thread.currentThread().getName()+"  Thread id信息:"+Thread.currentThread().getId());
    }
}

Implement the Runable method

package com.example.demoproject;

public class ThreadRun implements Runnable {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ThreadRun(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println("ThreadRun方法名称:"+Thread.currentThread().getName()+" Thread id信息:"+Thread.currentThread().getId());
    }
}

After contacting thread creation, we know that there are two ways to start threads: start() and run(). What is the difference between these two methods? Let's run the program to see.

package com.example.demoproject;

public class ThreadTest {

    public static void main(String[] args) {

        ThreadRun runThread = new ThreadRun("runThread");
        ThreadEx threadEx1 = new ThreadEx("start");
        ThreadEx threadEx2 = new ThreadEx("run");

        runThread.run();

        threadEx1.start();
        threadEx2.run();
    }
}

Program running results:

ThreadRun方法名称:main Thread id信息:1
运行方式run  ThreadEx方法名称:main  Thread id信息:1
运行方式start  ThreadEx方法名称:start  Thread id信息:11

Process finished with exit code 0

As you can see, there are a total of two threads. The IDs of the threads are 1 and 11. RunThread implements the run() method, but there is no start() method.

We can see that the thread id of the main() method is 1, and the thread id of the two threads started using the run() method is also 1. This shows what? Using the run() method does not create a new thread!

It is the thread started by the start() method. The thread id is 11, which is not the same thread as main(). Obviously, a new thread is started.

Therefore, using the run() method to run a thread is equivalent to creating an object and then calling a method of the object. There is no thread creation operation. A new thread will only be created when it is started with start().

Of course, in actual coding, it is rare to create threads by yourself, because frequent creation and destruction of threads is not a small overhead for services. We generally call threads in the thread pool to serve us.

By the way, look at the results of the operation, do you feel anything wrong?

For example, in my code, the start() method is written first. Why is the start() method at the end when it can be run? Why not come in order? You can try it and find that most of the situations are consistent with my running results.

Here, first of all, the start() method will rebuild a thread. This operation will undoubtedly be time-consuming, but this does not affect the operation of the main() method, so when the new thread is created, the main() method has been run When it comes to the following code, the start() method will naturally appear slower.

 

No sacrifice, no victory!

Guess you like

Origin blog.csdn.net/zsah2011/article/details/107238233