工作中用到了Thread,一开始用错了,仔细研究了一下,稍作整理。
当然我们的主要实现是在run里面,这也是Thread唯一implements Runnable的一个方法。
于是我们调用start方法,可以在新线程中执行run中的处理逻辑,于是会一位start方法调用了run方法
然后却又不是这样
start的方法代码
前言,今天写代码居然这样写的
new Thread() {
@Override
public void run() {
System.out.println("test");
}
}.run();
当然我们的主要实现是在run里面,这也是Thread唯一implements Runnable的一个方法。
于是我们调用start方法,可以在新线程中执行run中的处理逻辑,于是会一位start方法调用了run方法
然后却又不是这样
start的方法代码
/**
* Starts the new Thread of execution. The <code>run()</code> method of
* the receiver will be called by the receiver Thread itself (and not the
* Thread calling <code>start()</code>).
*
* @throws IllegalThreadStateException if the Thread has been started before
*
* @see Thread#run
*/
public synchronized void start() {
if (hasBeenStarted) {
throw new IllegalThreadStateException("Thread already started."); // TODO Externalize?
}
hasBeenStarted = true;
VMThread.create(this, stackSize);
}