启动线程的正确姿势
通过之前的分析,我们知道了有两种定义线程执行逻辑的方法,而创建线程只有一种方法,也就是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)
报错了,为什么不能多次调用start()
呢?我们来看一下start()
源码。
扫描二维码关注公众号,回复:
9181928 查看本文章
/**
* 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 */
}
}
}
从源码可以知道,调用start()
时,会判断threadStatus != 0
,很明显,刚刚的异常就是这个地方抛出来的,通过源码可以知道threadStatus
是记录线程的状态,初始默认为0
,当线程启动后,该线程的状态就改变了,所以threadStatus
的值也就改变了,当第二次调用start()
时,就会抛出异常。