Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对线程行为有点困惑_Java_Multithreading - Fatal编程技术网

Java 对线程行为有点困惑

Java 对线程行为有点困惑,java,multithreading,Java,Multithreading,我已经在线程中编写了一个基本代码,我得到的输出非常令人惊讶 public class ThreadImp implements Runnable{ public static void main(String[] args) { ThreadImp threadImp = new ThreadImp(); Thread t =new Thread(threadImp); t.setName("Fred");

我已经在线程中编写了一个基本代码,我得到的输出非常令人惊讶

public class ThreadImp implements Runnable{

    public static void main(String[] args) {
        ThreadImp threadImp = new ThreadImp();
        Thread t =new Thread(threadImp);
        t.setName("Fred");
        t.start();
        threadImp.run();
        t.run();
    
    }
    public void run(){
        System.out.println("Current Thread: "+ Thread.currentThread());
    }
}
我希望这里的输出是打印出来的

当前线程:线程[main,5,main]

当前线程:线程[Fred,5,main]

当前线程:线程[main,5,main]

这个结果我可以理解,我只创建了一个线程,即线程Fred。对run方法的另外两个调用与调用普通方法一样

但我不明白的是,有时当我执行同一个程序时,我会得到以下输出。有人能解释一下为什么会这样吗

当前线程:线程[main,5,main]

当前线程:线程[Fred,5,main]

您可以调用
Thread.run()
(通常不应该直接调用),如果
线程已完成并清理完毕,则此操作将不起任何作用:

来源:Java7更新79

其中,
target
是提供的可运行的。线程结束时,
target
字段设置为
null

来源:Java7更新79


因此,确切的输出(包括顺序,如果您收到两条或三条消息)取决于时间安排、日程安排等。

具体表现为什么?执行不一致?在
t.start()
之后添加
t.join()
,最重要的是要注意Java不是线程安全的,这意味着您不应该在设计程序时假设线程每次都以相同的方式运行。线程的行为方式也可能因机器而异。我想说的是,应该有3个方法调用来运行方法,并且print语句应该执行三次。但是当我再次运行它时,run方法只调用了两次。当运行结束时,线程是否将
null
设置为
target
?你能在哪里分享这段代码吗?@PabloMatiasGomez AddedAha<代码>多次启动线程是不合法的。特别是,线程一旦完成执行,就不能重新启动。
@fukanchik当然可以,但是
thread.start()
thread.run()
;)之间也有区别@谢谢,这解决了我的疑问。
@Override
public void run() {
    if (target != null) {
        target.run();
    }
}
/**
 * This method is called by the system to give a Thread
 * a chance to clean up before it actually exits.
 */
private void exit() {
    if (group != null) {
        group.threadTerminated(this);
        group = null;
    }
    /* Aggressively null out all reference fields: see bug 4006245 */
    target = null;
    /* Speed the release of some of these resources */
    threadLocals = null;
    inheritableThreadLocals = null;
    inheritedAccessControlContext = null;
    blocker = null;
    uncaughtExceptionHandler = null;
}