gpt4 book ai didi

java - 在 Java 中,如果一个线程被杀死,另一个线程会发生什么?

转载 作者:搜寻专家 更新时间:2023-10-31 19:30:13 25 4
gpt4 key购买 nike

我想用Java知道:

  1. 如果主线程被杀死,其他子线程会怎样?
  2. 如果子线程被杀死,兄弟线程和父线程会怎样?

我在以下链接中读到,由于线程共享地址空间,杀死一个线程也会影响其他线程。

以下引自 here .

Threads are light weight processes that divide main flow of control into multiple flows and each flow of control/thread will execute independently. Activity of the process in a system is represented by threads. The process that has multiple threads is called as multi threaded. Each threads has its own thread ID ( Data Type Integer), register, program counter, stack, error no. Threads can communicate using shared memory within same process. There are different advantages of using threads to mange and maintain the subtask of applications. When we are using threads than less system resources are used for context switching and increased the throughput of application. Threads also simplify the structure of program. There is no special mechanism for communication between tasks. Threads also have some disadvantages for example threads are not reusable as they are dependent on a process and cannot be separated from the process. Threads are not isolated as they don't have their own address space. The error cause by the thread can kill the entire process or program because that error affects the entire memory space of all threads use in that process or program. Due to the shared resources by the threads with in the process can also affect the whole process or program when a resource damage by the thread. For concurrent read and write access to the memory thread will required synchronizations. Data of the process can easily damage by the thread through data race because all the threads with in the process have write access to same piece of data.

你能告诉我上面链接中的任何内容是否适用于 java

最佳答案

1) “子线程”不会发生任何事情......

2) “兄弟线程”不会发生任何事情......

...有以下异常(exception):如果所有剩余线程都是守护线程,应用程序将终止(即,当只剩下守护线程时,它们也将被杀死)。

来自documentation of Thread :

[...] The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called [...]
  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

关于java - 在 Java 中,如果一个线程被杀死,另一个线程会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8180395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com