[quote]
[color=red]public void start()[/color]
[color=blue]Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. [/color]
The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
Throws:
IllegalThreadStateException - if the thread was already started.
[color=red]public void run()[/color]
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; [color=blue]otherwise, this method does nothing and returns.[/color]
Subclasses of Thread should override this method.
Specified by:
run in interface Runnable
[/quote]
上面是Thread类中Start()方法与run()方法的定义。
现有自定义的类如下:
[code="java"]
public class Test2 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("in runnable() run().");
}
});
Thread t2 =new MyThread();
t1.start();
t2.start();
}
static class MyThread extends Thread {
@Override
public void run() {
System.out.println("in MyThread() run().");
}
}
}[/code]
由于t2并没有指定target,即没有这样定义[code="java"]Thread t2 = new Thread(new MyThread());[/code]所以根据API中run()方法的定义,t2.start()方法的调用应该没有调用MyThread的run()方法。
Thread类的run() 源代码如下:[code="java"]public void run() {
if (target != null) {
target.run();
}
}[/code]
Thread类无参构造函数
[code="java"]public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}[/code][code="java"]private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not /
/ If there is a security manager, ask the security manager
what to do. /
if (security != null) {
g = security.getThreadGroup();
}
/ If the security doesn't have a strong opinion of the matter
use the parent thread group. /
if (g == null) {
g = parent.getThreadGroup();
}
}
/ checkAccess regardless of whether or not threadgroup is
explicitly passed in. /
g.checkAccess();
/
* Do we have the required permissions?
/
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
}
g.addUnstarted();
this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
this.name = name.toCharArray();
if (security == null || isCCLOverridden(parent.getClass()))
this.contextClassLoader = parent.getContextClassLoader();
else
this.contextClassLoader = parent.contextClassLoader;
this.inheritedAccessControlContext = AccessController.getContext();
this.target = target;
setPriority(priority);
if (parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/ Stash the specified stack size in case the VM cares /
this.stackSize = stackSize;
/ Set thread ID */
tid = nextThreadID();
}[/code]
但是根据API中start()方法的说明,run()方法是由JVM调用的,这是不是矛盾了?
求解答,谢谢啦。