The difference between start () and run () in thread in Java Multithreaded Series Foundation article

Source: Internet
Author: User

1. The difference between start () and run ()
start():启动一个新线程,新线程会执行相应的run()方法。start()不能被重复调用(会抛出异常)。run()  
2. Start () source (by jdk1.8)
public synchronized void start() {                  //判断状态是否为 New ;        if (threadStatus != 0)            throw new IllegalThreadStateException();        // 将线程添加到ThreadGroup中        group.add(this);        boolean started = false;        try {            // 通过start0()启动线程            start0();            // 设置started标记            started = true;        } finally {            try {                if (!started) {                    group.threadStartFailed(this);                }            } catch (Throwable ignore) {            }        }    }
3. How does invoking the start () method from an instance object of thread actually start the thread? Here are some simple additions to the way they are implemented.
    1. Based on the kernel thread (KLT) mapping to implement: KLT is a kernel thread, the kernel thread is directly scheduled to be switched by the OS, it is only an interface relative to the thread of the application, and the external program uses a lightweight process (light Weight processes, LWP) to make one-to-one interface calls with KLT. That is, the process internally attempts to take advantage of the kernel thread of the OS to participate in the actual dispatch, and uses the API call to interact with its own program as an intermediate bridge.
    2. User Thread,ut based implementation: This approach is to consider whether there can be no intermediate layer of the mapping, its own thread directly from the CPU to dispatch, perhaps theoretically more efficient. However, this implementation, the user process needs to focus on the level of abstraction is lower, skip the OS closer to the CPU, that is, to do a lot of os do things, the natural OS scheduling algorithm, create, destroy, context switch, hang, etc. to do their own (because the CPU only do the calculation). This is obviously a hassle, and many people have tried it and later gave up.
    3. Hybrid implementation: It is designed to retain the original schema of the kernel thread, and to use the user thread, the lightweight process remains the same as the kernel thread one by one, the only change is that the lightweight process is no longer tied directly to the process, but is linked to the user thread, User threads do not necessarily have to correspond to lightweight process one by one, but are many-to-many, as with a lightweight process list, which adds a layer of decoupling between lightweight processes and the original process, which may make scheduling more flexible.

In the previous JDK version, tried to use UT to implement, but later gave up, with the kernel thread corresponding to the way, as to some details, with the specific platform has a great relationship, the JVM will take due account of the specific platform of the factors to achieve, in the JVM specification does not stipulate how to implement, So for programmers, just know that in the new thread (), after invoking the start () method, there is theoretically a thread that can be dispatched by the OS.

The difference between start () and run () in thread in Java Multithreaded Series Foundation article

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.