Which may cause the EDT to not (re) run

I am looking for possible reasons for the EDT to shut down and not restart. More specific, I have a test suite in which sometimes one of the tests suffers from a timeout. A stream dump is always very similar to the following (where I removed some irrelevant lines from the main stream):

Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.79-b02 mixed mode):

"Attach Listener" daemon prio=10 tid=0x00007f7c60001000 nid=0x5d0f runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"main-SharedResourceRunner" daemon prio=10 tid=0x00007f7c908e6000 nid=0x5ce6 in Object.wait() [0x00007f7c8416a000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000eed580f0> (a jogamp.opengl.SharedResourceRunner)
    at java.lang.Object.wait(Object.java:503)
    at jogamp.opengl.SharedResourceRunner.run(SharedResourceRunner.java:252)
    - locked <0x00000000eed580f0> (a jogamp.opengl.SharedResourceRunner)
    at java.lang.Thread.run(Thread.java:745)

"AWT-XAWT" daemon prio=10 tid=0x00007f7c9085d000 nid=0x5ce3 runnable [0x00007f7c8456e000]
   java.lang.Thread.State: RUNNABLE
    at sun.awt.X11.XToolkit.waitForEvents(Native Method)
    at sun.awt.X11.XToolkit.run(XToolkit.java:541)
    at sun.awt.X11.XToolkit.run(XToolkit.java:505)
    at java.lang.Thread.run(Thread.java:745)

"Java2D Disposer" daemon prio=10 tid=0x00007f7c90840800 nid=0x5ce2 in Object.wait() [0x00007f7c8466f000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000eee2f878> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
    - locked <0x00000000eee2f878> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
    at sun.java2d.Disposer.run(Disposer.java:145)
    at java.lang.Thread.run(Thread.java:745)

"Service Thread" daemon prio=10 tid=0x00007f7c9009b800 nid=0x5cdc runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=10 tid=0x00007f7c90099800 nid=0x5cdb waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=10 tid=0x00007f7c90096800 nid=0x5cda waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x00007f7c90094000 nid=0x5cd9 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=10 tid=0x00007f7c90072000 nid=0x5cd8 in Object.wait() [0x00007f7c94e73000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000eee34650> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
    - locked <0x00000000eee34650> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" daemon prio=10 tid=0x00007f7c90070000 nid=0x5cd7 in Object.wait() [0x00007f7c94f74000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000eedcc110> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:503)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
    - locked <0x00000000eedcc110> (a java.lang.ref.Reference$Lock)

"main" prio=10 tid=0x00007f7c9000c000 nid=0x5cd1 in Object.wait() [0x00007f7c99c20000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000eb3adf98> (a java.awt.EventQueue$1AWTInvocationLock)
    at java.lang.Object.wait(Object.java:503)
    at java.awt.EventQueue.invokeAndWait(EventQueue.java:1282)
    - locked <0x00000000eb3adf98> (a java.awt.EventQueue$1AWTInvocationLock)
    at java.awt.EventQueue.invokeAndWait(EventQueue.java:1263)


"VM Thread" prio=10 tid=0x00007f7c9006b800 nid=0x5cd6 runnable 

"GC task thread#0 (ParallelGC)" prio=10 tid=0x00007f7c90022000 nid=0x5cd2 runnable 

"GC task thread#1 (ParallelGC)" prio=10 tid=0x00007f7c90023800 nid=0x5cd3 runnable 

"GC task thread#2 (ParallelGC)" prio=10 tid=0x00007f7c90025800 nid=0x5cd4 runnable 

"GC task thread#3 (ParallelGC)" prio=10 tid=0x00007f7c90027800 nid=0x5cd5 runnable 

"VM Periodic Task Thread" prio=10 tid=0x00007f7c900ae800 nid=0x5cdd waiting on condition 

JNI global references: 297

Note that the stream mainuses EventQueue.invokeAndWaitto schedule something on the EDT, but there is no EDT in the stream dump. And not a single EDT means that the thread mainwill wait forever because it Runnablenever ends.

, , EDT Runnable, . , EDT .

, . , . CI, , , - .

, , , . , EDT, EDT Runnable, , :

import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;


public class Test {
  public static void main(String[] args) throws InvocationTargetException, InterruptedException {
    try {
      EventQueue.invokeAndWait(new Runnable() {
        @Override
        public void run() {
          throw new RuntimeException("exception");
        }
      });
    } catch (Exception e) {
      //ignore
    }
    EventQueue.invokeAndWait(
        new Runnable() {
          @Override
          public void run() {
            System.out.println("EDT restarted");
          }
        }
    );
    System.out.println("All runnables finished");
  }
}

, UncaughtExceptionHandler, EDT , UncaughtExceptionHandler. , EDT. CI System.err System.out, -. .


Edit

, 100% .

@Test
public void testSomethingWhichInteractsWithTheEDT(){
  doStuffOnMainThread();
  //do or check something on the EDT
  EventQueue.invokeAndWait( new Runnable(){...});
  doMoreStuffOnMainThread();
  //do or check something else on the EDT
  EventQueue.invokeAndWait( new Runnable(){...});
}

EventQueue.invokeAndWait( new Runnable(){...});

, , , "AWT-EventQueue-0".

Swing, . , JPanel, JFrame.

, :

  • JVM , EDT, , . , . , JVM - Swing, .
  • Runnable, EDT, invokeAndWait. , , EDT , , Runnable "" EDT.

, , , Swing concurrency. , , , EDT . , EDT ( - ), AFAIK EventQueue#invokeAndWait.

+4
1

@mKorbel , , . Swing, .

@Robin

  • EDT
  • invokeAndWait EventQueue.isDispatchThread/SwingUtilities.isEventDispatchThread, RepaintManager
  • , ( , : -)

import java.awt.EventQueue;
import java.awt.TextField;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;
import javax.swing.*;

public class IsThereEDT {

    private ScheduledExecutorService scheduler;
    private AccurateScheduledRunnable periodic;
    private ScheduledFuture<?> periodicMonitor;
    private int taskPeriod = 30;
    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    private Date dateRun;

    public IsThereEDT() {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        periodic = new AccurateScheduledRunnable() {

            private final int ALLOWED_TARDINESS = 200;
            private int countRun = 0;
            private int countCalled = 0;

            @Override
            public void run() {
                countCalled++;
                if (this.getExecutionTime() < ALLOWED_TARDINESS) {
                    countRun++;
                    isThereReallyEDT(); // non on EDT
                }
            }
        };
        periodicMonitor = scheduler.scheduleAtFixedRate(periodic, 0, taskPeriod, TimeUnit.SECONDS);
        periodic.setThreadMonitor(periodicMonitor);
        isThereReallyEDT();
        //TextField text = new TextField();
        //JFrame frame1 = new JFrame("Frame 1");
        /*SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {                
                isThereReallyEDT();
                JFrame frame1 = new JFrame("Frame 1");
                frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame1.getContentPane().add(new JLabel("Hello in frame 1"));
                frame1.pack();
                frame1.setLocation(100, 100);
                frame1.setVisible(true);
            }
        });*/
        /*SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame2 = new JFrame("Frame 2");
                frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame2.getContentPane().add(new JLabel("Hello in frame 2"));
                frame2.pack();
                frame2.setLocation(200, 200);
                frame2.setVisible(true);
                isThereReallyEDT();
            }
        });*/
        isThereReallyEDT();
    }

    private void isThereReallyEDT() {
        dateRun = new java.util.Date();
        System.out.println("                         Time at : " + sdf.format(dateRun));
        if (EventQueue.isDispatchThread()) {
            System.out.println("Calling from EventQueue.isDispatchThread");
        } else {
            System.out.println("There isn't Live EventQueue.isDispatchThread, why any reason for that ");
        }
        if (SwingUtilities.isEventDispatchThread()) {
            System.out.println("Calling from SwingUtilities.isEventDispatchThread");
        } else {
            System.out.println("There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
          IsThereEDT isdt = new IsThereEDT();
        /*SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setVisible(true);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
                frame.dispose();
                JOptionPane.showMessageDialog(frame, "Test case 1, "
                        + "JVM won't terminate.");
            }
        });*/
    }
}

abstract class AccurateScheduledRunnable implements Runnable {

    private ScheduledFuture<?> thisThreadsMonitor;

    public void setThreadMonitor(ScheduledFuture<?> monitor) {
        this.thisThreadsMonitor = monitor;
    }

    protected long getExecutionTime() {
        long delay = -1 * thisThreadsMonitor.getDelay(TimeUnit.MILLISECONDS);
        return delay;
    }
}

( (), )

             Time at : 11:03:50 There isn't Live EventQueue.isDispatchThread, why any reason for that  There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that 

             Time at : 11:04:20 There isn't Live EventQueue.isDispatchThread, why any reason for that  There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that 

             Time at : 11:04:50 There isn't Live EventQueue.isDispatchThread, why any reason for that  There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that
+2

Source: https://habr.com/ru/post/1608442/


All Articles