我有几个关于 Java 多线程的问题。我目前正在学习多线程的不同方法。我的第一个问题是,线程中的代码运行完毕后会发生什么?我需要停止/终止线程吗?我目前正在为每个线程创建一个类,并在每个类中实现 Runnable。然后我使用 new ThreadClass();.
在主类中启动线程在 Thread 类的构造函数中,我将其设置为创建一个名为“second”的线程。如果我添加 new ThreadClass()
在主方法中两次,两个线程都名为“第二”吗?谢谢。
最佳答案
My first question is, what happens to the thread after the code in it is done running? Do I need to Stop/Kill the thread?
线程无事可做时停止。如果您有 ExecutorService,则必须在使用完毕后使用 shutdown。
If I add new ThreadClass() twice in the main method, are both threads named "second"?
您正在使代码相同。这并不意味着线程的名称必须相同(反之亦然)
关于java - 对多线程概念有点困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14203790/