在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口。
以下就是我们常见的问题了:
1、
为什么我们不能直接调用run()方法呢?
我的理解是:线程的运行需要本地操作系统的支持。
如果你查看start的源代码的时候,会发现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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 || this != me)
throw new IllegalThreadStateException();
group.add( this );
start0();
if (stopBeforeStart) {
stop0(throwableFromStop);
}
}
private native void start0();
|