本文介绍了Java中main()方法和主线程之间有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的导师告诉我,主线程是每个线程的父线程,但他无法解释原因.

My tutor told me that the main thread is the parent thread of every thread, but he is not able to explain why.

当我编写一个简单的程序时:

When I write a simple program:

Class A{}

然后在执行时将其引发异常:

Then it at the time of execution it throws an exception:

java.lang.NoSuchMethodError: main Exception in thread "main"

main()方法和主线程之间是否存在任何关系?

Is there any relation between the main() method and the main thread?

推荐答案

当JVM启动时,它将创建一个名为"Main"的线程.除非您自己创建其他线程,否则程序将在此线程上运行.

When the JVM starts, it creates a thread called "Main". Your program will run on this thread, unless you create additional threads yourself.

主"线程要做的第一件事是查找您的static void main(String[] argv)方法并调用它.那是程序的切入点.

The first thing the "Main" thread does is to look for your static void main(String[] argv) method and invoke it. That is the entry-point to your program.

如果您希望事情同时发生",则可以创建多个线程,并给每个线程执行的任务.然后,他们将继续同时执行这些操作. JVM还为后台工作创建了一些内部线程,例如垃圾回收.

If you want things to happen "at the same time", you can create multiple threads, and give each something to execute. They will then continue to do these things concurrently. The JVM also creates some internal threads for background work such as garbage collection.

这篇关于Java中main()方法和主线程之间有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:27