0

我已经实现了一个应用程序,它正在创建新线程并对其执行操作。我一直在想,如果某个线程如何挂起或无响应,那么如何释放该线程使用的分配内存和其他资源。

请注意,在我的申请中,我已经解决了以下问题。所以我想要一些更具体的专家级答案来杀死或释放线程。

1. Creating thread using Thread class
2. 3-synchronization blocks (using lock), minimal number of blocks
3. Thread safe members
4. carefully handled exceptions

在阅读了几篇专家的文章并实施了一些解决方案后,我了解到 Thread.abort() 在大多数情况下不应该使用。只有在我们知道当前正在使用的位置和状态或类型的资源时才应该使用它由那个线程。对我来说,这看起来像是一个无法存档的目标。

因为我们永远不知道线程实际挂在什么/哪里。无论是在移动时获得锁定还是正在读取/写入文件。

因此,如果我们在任何这些情况下终止/中止线程,它可能会使情况变得更糟。因此,非其他线程将能够获取该线程已使用的任何资源,并且应用程序将停止工作或崩溃。

看到很多人想出了自己的解决方案,这真是令人惊讶——尽管有很多危险因素需要同时进行。

我相信我们不能使用 thread.abort() (由于上述原因)。我们可以做的是防止或减少制作无响应线程的机会,

   1. reducing number of synchronization blocks
   2. proper I/O handling 
   3. proper exception handling 
   4. reducing piece of code inside sync blocks

在实现上述内容之后,如果我们得到一个挂起的线程,那么只能采取以下行动。

1. unload the app-domain (this will stop appn)
2. using flags and signaling to check exactly where is the thread and what is is doing and if all criteria are met then abort the thread. (i not sure about this)

请让我知道您在这方面的意见。非常感谢。

4

0 回答 0