chinaxiaoxi 2011-10-21 10:06
浏览 271
已采纳

关于Thread类中的start()方法和run()方法

[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调用的,这是不是矛盾了?
求解答,谢谢啦。

  • 写回答

3条回答 默认 最新

  • _1_1_7_ 2011-10-21 16:38
    关注

    [code="java"] static class MyThread extends Thread {

    @Override

    public void run() {

    System.out.println("in MyThread() run().");

    }

    } [/code]
    你已经覆盖了Thread 的run方法,就没有什么target的说法了[code="java"]public void run() {

    if (target != null) {

    target.run();

    }

    } [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题