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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵