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 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统