天天看点

启动线程的正确姿势

推荐:​​Java并发编程汇总​​

启动线程的正确姿势

通过之前的分析,我们知道了有两种定义线程执行逻辑的方法,而创建线程只有一种方法,也就是​

​new​

​​一个​

​Thread​

​​类的实例。

​JAVA实现多线程到底有多少种方法?​​

我们如何启动线程呢?其实很简单,调用​

​start()​

​即可。

package startThread;

public class test {
    public static void main(String[] args) {
        Runnable runnable = ()->{
            System.out.println(Thread.currentThread().getName());
        };

        Thread thread = new Thread(runnable);
        // 设置线程名称
        thread.setName("thread_kaven");
        thread.start();
    }
}      

输出:

thread_kaven      

很显然,​

​start()​

​​启动了线程,并且是在一个新的线程中去执行任务的,但是为什么一定要调用​

​start()​

​​,而不直接调用该线程的执行逻辑​

​run()​

​​呢?

我们来测试调用​​

​run()​

​的情况。

package startThread;

public class test {
    public static void main(String[] args) {
        Runnable runnable = ()->{
            System.out.println(Thread.currentThread().getName());
        };

        Thread thread = new Thread(runnable);
        // 设置线程名称
        thread.setName("thread_kaven");
        thread.run();
    }
}      

输出:

main      

很显然,调用​

​run()​

​​,并没有创建新的线程去执行任务,​

​run()​

​​还是在​

​main​

​线程中执行的。

我们可以得到一个简单的结论:

​run()​

​​只是一个类中的普通方法,调用​

​run()​

​​跟调用普通方法一样。

而调用​​

​start()​

​​,则会做一系列工作去创建新线程,然后在新线程中执行​

​run()​

​里面的任务内容。

我们可以多次调用​

​start()​

​吗?我们来测试一下。

package startThread;

public class test {
    public static void main(String[] args) {
        Runnable runnable = ()->{
            System.out.println(Thread.currentThread().getName());
        };

        Thread thread = new Thread(runnable);
        // 设置线程名称
        thread.setName("thread_kaven");
        thread.start();
        thread.start();
    }
}      

输出:

thread_kaven
Exception in thread "main" java.lang.IllegalThreadStateException
  at java.lang.Thread.start(Thread.java:708)
  at startThread.test.main(test.java:13)      
/**
     * Causes this thread to begin execution; the Java Virtual Machine
     * calls the <code>run</code> method of this thread.
     * <p>
     * The result is that two threads are running concurrently: the
     * current thread (which returns from the call to the
     * <code>start</code> method) and the other thread (which executes its
     * <code>run</code> method).
     * <p>
     * It is never legal to start a thread more than once.
     * In particular, a thread may not be restarted once it has completed
     * execution.
     *
     * @exception  IllegalThreadStateException  if the thread was already
     *               started.
     * @see        #run()
     * @see        #stop()
     */
    public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }