简体   繁体   English

如何在Java中执行线程

[英]How threads are executed in java

I have a code- 我有一个代码

public class ThreadOne
{
    public static void main(String[] args)
    {
        Thread1 th=new Thread1();
        Thread1 th2=new Thread1();
        th.start();
        th2.start();
        System.exit(1);
    }
}


class Thread1 extends Thread
{
    public void run()
    {
        for(int i=0;i<10;i++)
        {
            System.out.println(i);
        }
    }
}

What I wanted to know is- 我想知道的是-

  • Why the above code doesn't print anything? 为什么上面的代码不打印任何内容? Although I have created 2 threads and started them,yet it doesn't print anything.Why? 尽管我创建了2个线程并启动了它们,但是它什么也没打印。为什么?
  • If there is no user thread,for eg say a normal program consisting of many methods and each method is invoked in main() method,then how many threads are created by default,does it depend on the number of methods we have,or is there a single main thread which is responsible for invoking all methods,and at last who creates the main thread,is it JVM which creates it? 如果没有用户线程(例如,由许多方法组成的普通程序,并且每个方法都在main()方法中调用),那么默认情况下会创建多少个线程,这取决于我们拥有的方法数,或者有一个主线程负责调用所有方法,最后由谁创建主线程,是由JVM创建的吗?

System.exit(1); will terminate the currently running Java Virtual Machine . 将终止当前正在运行的Java虚拟机 When your program exit, your threads will also die. 当程序退出时,线程也将死亡。

Thread is a part of Process , If Process have exited, then all threads will be destroyed. ThreadProcess的一部分,如果Process退出,则所有线程将被销毁。

Thread.join() will wait until thread run finished. Thread.join()将等待,直到线程运行完成。

public class ThreadOne
{
    public static void main(String[] args)
    {
        Thread1 th=new Thread1();
        Thread1 th2=new Thread1();
        th.start();
        th2.start();
        th.join();
        th2.join();
        System.exit(1);
    }
}


class Thread1 extends Thread
{
    public void run()
    {
        for(int i=0;i<10;i++)
        {
            System.out.println(i);
        }
    }
} 
  1. Your code does not show anything because you are killing the app with System.exit() as soon as it starts. 您的代码没有显示任何内容,因为您将在System.exit()启动后立即终止该应用程序。 You should wait for both threads to complete before exiting, by using Thread.join() for example. 您应该等待两个线程完成再退出,例如使用Thread.join()。

  2. There is one thread by default, which executes your main() method and it is created by the JVM. 默认情况下有一个线程,该线程执行您的main()方法,并由JVM创建。

As thread is not Synchronized execution like normal java code. 由于线程不像普通的Java代码那样同步执行。 so after calling th.start(); 所以在调用th.start()之后; th2.start(); th2.start(); it won't wait for run() to complete thats why System.exit(1); 它不会等待run()完成,这就是System.exit(1);的原因。 called and you are getting anything. 打电话,你得到任何东西。

you can wait till the threads executes by using join.check the following 您可以等到通过使用join执行线程后再检查以下内容

 public class ThreadOne
 {
public static void main(String[] args)
{
    Thread1 th=new Thread1();
    Thread1 th2=new Thread1();
    th.start();
    th2.start();
    try {
        th.join();
        th2.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.exit(1);
}

} }

  class Thread1 extends Thread
{
public void run()
{
    for(int i=0;i<10;i++)
    {
        System.out.println(i);
    }
}

} }

According to java docs regarding system.exit 根据有关system.exit的Java文档

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

So above is your answer regarding your first question. 因此,以上是关于第一个问题的答案。 When JVM starts usually a single thread is created which calls main and then rest of your methods. JVM启动时,通常会创建一个线程,该线程先调用main,然后再调用其余方法。 Hope it helps. 希望能帮助到你。

To see the numbers being printed on screen add the following:

try{
       th.join();
       th2.join();
   }catch(Exception e){
}

After th2.start() in main method.

As others have pointed out you will need to give time to the user-defined threads to complete their work and this is done by calling join() on the respective thread objects.join() ensures that the calling thread "Waits for this thread to die" before proceeding ahead. 正如其他人指出的那样,您需要花时间给用户定义的线程完成其工作,这是通过在各个线程对象上调用join()来完成的。join()确保调用线程“等待该线程执行”。死”。

your 2 threads are started by the main threads, so there are 3 threads get invoked. 您的2个线程由主线程启动,因此有3个线程被调用。 but system.exit() kills your main thread causing other 2 threads to terminate before they get any chance to run. 但是system.exit()杀死了您的主线程,导致其他2个线程在没有机会运行之前终止。

Since the threads are separate from main: the code in main continues to be executed and System.exit(1); 由于线程与main是分开的:main中的代码继续执行,并且System.exit(1); executes without any regard for the Threads and shuts down the program. 在不考虑线程的情况下执行并关闭程序。

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

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