0

How to know that EDT is blocked (not visually but by inspecting the thread itself)? is there a way?

I'm on the way to my final university task for graduation that have bunch of charts , but have little knowledge of Swing EDT (Java generally).

have look at this piece:

 SwingUtilities.invokeLater(new Runnable() {
         public void run() {
             //  task here
         }
    });

If we do some tasks that modify gui component on the block (including calling another Thread) , is that already the correct approach?

And since EDT is single threaded environment, what else (at least) one part/example that blocking EDT other than calling Thread.start() directly inside the GUI. What method I should call to recognize that the EDT is blocked

Before I asked this, I've read some documentation (but it fails explains to me how to prevent blocking EDT in simple explanation.)

Thanks in advance

  • AFKAI There's no real way to know when the EDT is blocked. You can use `SwingUtilities.isEventDispatchThread()` to detect when you are executing in the EDT and should use `SwingWorker`, Swing `Timer` or `Thread`s to execute long running tasks outside of the EDT. `invokeLater` will execute the `Runnable` within the context of the EDT – MadProgrammer Jun 17 '15 at 02:21
  • @MadProgrammer Thanks for the response, if so then , Are there any examples that showing that we block EDT (google just confuse on some advance documentations.) – Plain_Dude_Sleeping_Alone Jun 17 '15 at 02:24
  • `Thread.sleep(1000)` called within the context of the EDT will block it from running for 1 second. – MadProgrammer Jun 17 '15 at 02:25
  • @MadProgrammer, uh I see , could you explain more in answer please, I know this comment box is pretty limited. – Plain_Dude_Sleeping_Alone Jun 17 '15 at 02:29
  • I'm still not sure what you're asking – MadProgrammer Jun 17 '15 at 02:39
  • @MadProgrammer, I get you actually , I just want to make sure that I understand the basic thing here, I'll accept any samples codes / or just explanations here. – Plain_Dude_Sleeping_Alone Jun 17 '15 at 02:46

1 Answers1

1

Apart from being blocked by Thread.sleep(), the EDT could e.g. be blocked when waiting for an asynchronous operation.

Here's an example that -- after the "Click & Wait" button is pressed -- blocks the EDT until the main thread can read text from the console. You will notice that the button stays pressed until the text is entered. Then the EDT resumes and updates the label:

CompletableFuture<String> future = new CompletableFuture<>();

SwingUtilities.invokeLater(() -> {
    JFrame f = new JFrame("EDT Blocking Example");
    f.setSize(200, 200);
    JLabel label = new JLabel();
    JButton button = new JButton("Click & Wait");
    button.addActionListener(l -> {
        try {
            // calling get() will block the EDT here...
            String text = future.get();
            label.setText(text);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    JPanel content = new JPanel();
    content.add(button);
    content.add(label);
    f.setContentPane(content);
    f.setVisible(true);
});
System.out.print("Enter some text: ");
String input = new Scanner(System.in).nextLine();

future.complete(input);

As for knowing when the EDT is blocked: This is tricky, but a profiler or a thread dump can help in analyzing synchronization issues.

Rahel Lüthy
  • 6,837
  • 3
  • 36
  • 51