【问题标题】:Problem getting threads to terminate and program to end execution让线程终止和程序结束执行的问题
【发布时间】:2020-05-16 15:28:28
【问题描述】:

我正在用 java 做一个简单的程序。 2 个线程应该将它们的 id 附加到一个字符串,然后第三个线程对整个字符串进行哈希处理。这应该发生5次。它确实有效,问题是程序只是继续运行并且不会停止执行。我试过break,但它仍然挂起。

我假设某种僵局正在发生,但我不明白为什么或如何。

public void run()
{
    while(numOfIterations > 0)
    {

        if (numOfIterations == 0){   
            break;
        }


        if(this.id == "A")
        {
            appendThread(this.id);

        }
        else if(this.id == "B")
        {
            appendThread(this.id);

        }
        else
        {
            hashThread();
        }

        try
        {
            Thread.sleep(interval);

            if(this.nextThread.isAlive())
            {
                nextThread.interrupt();
            }
            else
            {
                nextThread.start();
            }
            Thread.sleep(Long.MAX_VALUE);

        }
        catch (InterruptedException e)
        {
           System.out.println("Interrupted Thread : "+ this.iD);
        }
    }
}

注意:run 方法位于扩展 Thread 的类中,该类有一个名为 nextThread 的属性(基本上是对应该执行下一个任务的线程的引用。 thread1 -> thread2 -> thread3 -> thread1 ...)。

appendThread() 方法将 id 附加到字符串,hashThread对字符串进行哈希处理。 两种方法都将numOfIterations 变量减一

这是输出:

hashed word = b86fc6b051f63d73de262d4c34e3a0a9
numOfIterations : 4
Interrupted Thread : A
Interrupted Thread : B
Interrupted Thread : C
hashed word = a63e3d9e4d46287e71ec1248d1be5782
numOfIterations : 3
Interrupted Thread : A
Interrupted Thread : B
Interrupted Thread : C
hashed word = 444b8618908e49ca64c8eafab94add38
numOfIterations : 2
Interrupted Thread : A
Interrupted Thread : B
Interrupted Thread : C
hashed word = 535cfc91d9e96b57395000fdae460bf1
numOfIterations : 1
Interrupted Thread : A
Interrupted Thread : B
Interrupted Thread : C
hashed word = 3e9c9cfb98a356ff155fa67abbbaccb9
numOfIterations : 0
Interrupted Thread : A

输出是正确的,唯一的问题是程序没有结束。另外,请注意输出如何以 Interrupted Thread : A 结尾,而不是 B 或 C。

任何帮助将不胜感激。

【问题讨论】:

  • numOfIterations 不会减少并且始终 > 0。
  • 感谢您的评论,正如我所说,appendThreadhashThread 函数的 numOfIterations 减少了
  • @AsPas 是静态变量 numOfIterations 吗?
  • @AsPas 你必须把它作为 try 中的 if 语句之一来做 ////// if(this.nextThread.isAlive()) { nextThread.interrupt(); }else if (numOfIterations == 0){ break; } 其他 { nextThread.start(); }
  • edit您的问题并发布minimal reproducible example。然后,希望我可以重现您的问题,之后(再次希望)我可以找到您描述的行为的原因。

标签: java multithreading java-threads


【解决方案1】:

几个小时后,我才弄清楚发生了什么。 由于每个线程都在等待被前一个线程中断,所以在线程 B 和 C 等待线程 A 唤醒它们时发生了死锁,但 A 已经退出了函数。

Interrupted Thread : A 最后打印的原因是线程 A 确实终止了它的方法,但线程 B 和 C 仍在休眠。

所以我只需要通过调用 nextThread.interrupt() 在函数存在之前使线程 A 中断线程 B。这样A在退出方法之前唤醒B,B唤醒C,所以所有线程都可以退出函数。

将这个添加到函数的末尾解决了我的问题。

if (numOfIterations == 0){   
    nextThread.interrupt();
}

【讨论】:

    【解决方案2】:

    您必须将其作为 try 中的 if 语句之一:

    if(this.nextThread.isAlive()) { 
      nextThread.interrupt(); 
    }
    else if (numOfIterations == 0){ 
      break; 
    } else { 
    nextThread.start(); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-03
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多