Java中如何中断同步语句?

发布于 2024-08-22 04:26:57 字数 996 浏览 5 评论 0原文

我有两个线程想要在同一个对象上同步。如果满足特定条件,则线程 A 需要能够中断线程 B。这是两个线程做什么/应该做什么的一些伪代码。

A:

public void run()
{
    while(true)
    {
        //Do stuff
        synchronized(shared)
        {
            //Do more stuff
            if(condition)
            {
                B.interrupt();
            }
        }
    }
}

B:

public void run()
{
    while(true)
    {
        try
        {
            //Do stuff
            synchronized(shared)
            {
            //Do more stuff
            }
        }
        catch(InterruptedException e)
        {
            continue;
        }
    }
}

这是我无法解决的情况:

  • 线程 A 获取共享资源并执行一些操作。
  • 同时,线程B到达同步块,并等待A释放其共享资源。
  • 线程A在做事情时,意识到线程B不应该拥有共享资源,并尝试中断线程B。但线程 B 已经超出了可能抛出 InterruptedException 的范围。

我的问题是,有什么方法可以在线程等待某些内容同步时中断该线程吗?

I have two threads that want to synchonize on the same object. Thead A needs to be able to interrupt Thread B if a certain condition has been fullfilled. Here is some pseudo-code of what the two threads do/should do.

A:

public void run()
{
    while(true)
    {
        //Do stuff
        synchronized(shared)
        {
            //Do more stuff
            if(condition)
            {
                B.interrupt();
            }
        }
    }
}

B:

public void run()
{
    while(true)
    {
        try
        {
            //Do stuff
            synchronized(shared)
            {
            //Do more stuff
            }
        }
        catch(InterruptedException e)
        {
            continue;
        }
    }
}

Here's the situation I can't resolve:

  • Thread A grabs the shared resource and does some stuff.
  • Meanwhile, Thread B reaches the synchronized block, and awaits for A to release its shared resource.
  • Thread A, while doing stuff, realized that Thread B should not have the shared resource, and tries to interrupt Thread B. But Thread B has already surpassed the points where an InterruptedException could be thrown.

My question is, is there any way to interrupt a thread while it is waiting to be synchronized on something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヤ经典坏疍 2024-08-29 04:26:57

对于这种事情,您应该使用 java.util.concurrent.locks - 它们具有更多功能,包括可中断锁。

编辑:如果您无法使用这些类,请查看 jkff 的答案 - 您的要求可以通过 wait()/notify() 来满足机制,但很容易引入微妙的错误。

For this kind of thing, you should use the classes in java.util.concurrent.locks - they have far more capabilities, including interruptable locks.

Edit: If you cannot use those classes, then look at jkff's answer - your requirements can be met with the wait()/notify() mechnism, but it's easy to introduce subtle bugs.

北方。的韩爷 2024-08-29 04:26:57

事实上,您应该使用锁或使用 Object.wait()Object.notify()Object.notifyAll() 来实现您的东西方法(锁实际上是用它们实现的)。不要忘记处理所谓的“虚假唤醒”(即使没有人调用 notify()notifyAll()wait() 也可能返回>,因此应该始终在循环中调用它来检查您正在等待的条件是否得到满足)。

Indeed you should use the locks or implement your stuff with the Object.wait(), Object.notify() and Object.notifyAll() methods (locks are actually implemented with them). Do not forget to handle the so-called 'spurious wakeups' (wait() may return even if noone called notify() or notifyAll(), so it should always be called in a loop that checks that the condition you're waiting for is satisfied).

不再让梦枯萎 2024-08-29 04:26:57

不,但是 ReentrantLock.lockInterruptically() 的行为与原始 monitorenter 指令类似,并且可以被中断。

No, but ReentrantLock.lockInterruptibly() behaves similarly to the primitive monitorenter instruction and can be interrupted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文