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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?