简体   繁体   English

基本的Java线程和可运行模式

[英]Basic Java threading and runnable pattern

I have a problem with understanding this code. 我对理解这段代码有疑问。 I have only a couple of hours' knowledge of Java. 我只有几个小时的Java知识。

Here is the code : 这是代码:

// Create a new thread.
class NewThread implements Runnable {
   Thread t;
   NewThread() {
      // Create a new, second thread
      t = new Thread(this, "Demo Thread");
      System.out.println("Child thread: " + t);
      t.start(); // Start the thread
   }

   // This is the entry point for the second thread.
   public void run() {
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Child Thread: " + i);
            // Let the thread sleep for a while.
            Thread.sleep(50);
         }
     } catch (InterruptedException e) {
         System.out.println("Child interrupted.");
     }
     System.out.println("Exiting child thread.");
   }
}

public class ThreadDemo {
   public static void main(String args[]) {
      new NewThread(); // create a new thread
      try {
         for(int i = 5; i > 0; i--) {
           System.out.println("Main Thread: " + i);
           Thread.sleep(100);
         }
      } catch (InterruptedException e) {
         System.out.println("Main thread interrupted.");
      }
      System.out.println("Main thread exiting.");
   }
}

And here is output of it : 这是输出:

Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.

Here are my questions. 这是我的问题。

I want to understand the pattern the code follows. 我想了解代码所遵循的模式。 According to me, 据我说,

  • First of all, the Program should start to execute the main() function. 首先,程序应该开始执行main()函数。 So, instance of NewThread should be initialized. 因此,应该初始化NewThread的实例。
  • Then, I have to go into NewThread constructor and write Child thread: Thread[Demo Thread,5,main] 然后,我必须进入NewThread构造函数并编写Child thread: Thread[Demo Thread,5,main]
  • After that t.start() comes and therefore Program should execute public void run() (AM I WRONG HERE ?? ) 之后t.start()来了,因此程序应该执行public void run() (我错了吗?)

In public void run() , I think I should have gotten an output Child Thread 5 , but instead I got Main Thread 5 . public void run() ,我想我应该得到一个输出Child Thread 5 ,但是我得到了Main Thread 5 I wonder why ?? 我想知道为什么 ??

Is there anyone to help me ?? 有没有人帮我? Thanks in advance. 提前致谢。

t.start() creates a new thread, and calls run() from that. t.start()创建一个新线程,并从中调用run() At that point, there are two threads running independently: the one that called start() , and the new one. 此时,有两个独立运行的线程:一个调用start() ,另一个调用新线程。 The original thread returns from the constructor and then starts executing the loop in the main() method. 原始线程从构造函数返回,然后在main()方法中开始执行循环。

As the two threads are independent, there's no guarantee which thread will reach a call to System.out.println first. 由于这两个线程是独立的,因此无法保证哪个线程首先会调用System.out.println In the sample output that you've given, it so happens that the original thread gets to print first. 在您给出的示例输出中,原始线程首先打印出来。 It could easily happen the other way round though. 反过来可能很容易发生。

As an aside, if you're new to Java I would suggest you learn the basics of the language before you get to threading. 顺便说一句,如果您是Java新手,我建议您进入线程之前先学习一下该语言的基础知识。 There's nothing in your question which indicates that you're confused, but threading is a relatively advanced topic, and it's worth being comfortable with the general language behaviour before you get that far, IMO. 您的问题中没有任何内容表明您感到困惑,但线程是一个相对高级的主题,并且在您达到目标之前,值得对一般语言行为感到满意,IMO。 That way you can be confident that any odd behaviour you see really is due to threading and not to misunderstanding other parts of the language. 这样你就可以确信你看到的任何奇怪行为都是由于线程而不是误解语言的其他部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM