简体   繁体   English

新线程和调用线程将被阻塞Java信号量

[英]New thread and calling thread will be blocked Java Semaphore

I have some trouble with my java swing program. 我的java swing程序遇到一些麻烦。 I try to stop my main Frame thread when the ExportWithoutEntryPointFrm Frame appears in an own thread. ExportWithoutEntryPointFrm框架出现在自己的线程中时,我尝试停止我的主框架线程。 I implemented that with java.util.concurrent.Semaphore . 我用java.util.concurrent.Semaphore实现了这一点。 The appearing Frame shows only an empty Frame, the buttons, lables and so on won´t be shown and both threads are blocked. 出现的“框架”仅显示一个空框架,按钮,标签等将不会显示,并且两个线程均被阻塞。 I think there is a deadlock but I don´t find it. 我认为这是一个僵局,但我找不到。

My code for the new warning Frame, which will be called from the main Frame: 我的新警告框架代码,将从主框架中调用:

public class ExportWithoutEntryPointFrm extends javax.swing.JFrame implements Runnable
{

    private Semaphore sema;
    private boolean decision = false;


    public ExportWithoutEntryPointFrm(Semaphore semaphore)
    {
        initComponents();
        this.setLocationRelativeTo(null);
        this.sema = semaphore;

    }

    @Override
    public void run()
    {
        this.setVisible(true);

        try
        {
            sema.acquire();

        }
        catch (InterruptedException e)
        {
            this.decision = false;
            this.sema.release();

            this.setVisible(false);

        }
    }
}

And the calling code from the main Frame: 以及来自主框架的调用代码:

Semaphore waitForDecisionSema = new Semaphore(1, true);

ExportWithoutEntryPointFrm warningFrm = new ExportWithoutEntryPointFrm(waitForDecisionSema);

warningFrm.run();
waitForDecisionSema.acquire();

First of all, calling a run() method of a Runnable doesn't start a new thread. 首先,调用Runnable的run()方法不会启动新线程。

Second, even if it did, Swing components like JFrame MUST be used from the event dispatch thread only. 其次,即使这样做,也必须仅从事件分发线程中使用Swing组件(如JFrame)。

Third: since everything is done from a single thread, the EDT, as soon as this line is executed: 第三:由于所有操作都是从单个线程完成的,因此在执行此行后,EDT就会立即执行:

waitForDecisionSema.acquire();

the EDT is blocked waiting for some other thread to release the semaphore, and that will never happen, so the EDT is blocked forever, making your GUI unresponsive. EDT被阻止,等待其他线程释放信号,这将永远不会发生,因此EDT被永久阻止,从而使GUI无响应。

You seriously need to reconsider your design. 您非常需要重新考虑您的设计。 But I don't know what you're trying to achieve, so it's hard to advise. 但是我不知道您要达到什么目标,因此很难提出建议。 Given the name of your semaphore, I think that what you're looking for is a modal JDialog, that would prevent the user to use the parent frame of the dialog until the dialog is closed. 给定信号量的名称,我认为您正在寻找的是模式JDialog,它将阻止用户使用对话框的父框架,直到对话框关闭。

I try to stop my main Frame thread when the ExportWithoutEntryPointFrm Frame appears in an own thread 当ExportWithoutEntryPointFrm框架出现在自己的线程中时,我尝试停止我的主框架线程

Well, that's a massive contradiction in terms, Swing is a single threaded framework, you can operate components/frames/windows in separate threads, it won't work and you'll end up within no end of issues, dead locks been the most obviously. 好吧,这是一个巨大的矛盾,Swing是一个单线程框架,您可以在单独的线程中操作组件/框架/窗口,它将无法正常工作,并且您将无休无止,死锁是最多的明显。

Start by having a look at Concurrency in Swing for more details. 首先查看Swing中的并发以获取更多详细信息。

Now, there a number of mechanisms you can use to off load long running or blocking code to a separate thread and still interact with Swing, a Swing Timer for regular scheduled callbacks, SwingWorker for long running or potentially blocking calls, but which supports callbacks to the EDT, making it easy to use and even SwingUtilities.invokeLater for those times you have no other choice. 现在,您可以使用多种机制将长时间运行或阻塞的代码卸载到单独的线程上,并仍与Swing交互; Swing Timer用于常规调度的回调; SwingWorker用于长时间运行或可能阻塞的调用,但支持回调到EDT,在那些时候您别无选择,甚至易于使用,甚至SwingUtilities.invokeLater

Have a look at How to use Swing Timers and Worker Threads and SwingWorker for more details 看看如何使用Swing计时器辅助线程以及SwingWorker以获得更多详细信息

Based on you description though, I would suggest that what you really want, is a modal dialog, which will block the current frame/code execution at the point the dialog is made visible, but which will allow the UI to continue responding to the user. 不过,根据您的描述,我建议您真正想要的是一个模式对话框,它将在显示对话框的那一刻阻止当前帧/代码的执行,但将允许UI继续响应用户。

See How to Make Dialogs for more details 有关更多详细信息,请参见如何制作对话框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM