EventQueue inconsistent ID's

一曲冷凌霜 提交于 2020-01-13 11:08:10

问题


I have a problem with the following example code that shows inconsistent behavior for the EventQueue:

public static void main( String[] args ) throws InvocationTargetException, InterruptedException {

    final long[] id1 = new long[ 1 ];
    final long[] id2 = new long[ 1 ];

    EventQueue.invokeAndWait( new Runnable() {
      @Override public void run() {
        id1[ 0 ] = Thread.currentThread().getId();
      }
    } );

    Thread.sleep( 5000 );

    EventQueue.invokeAndWait( new Runnable() {
      @Override public void run() {
        id2[ 0 ] = Thread.currentThread().getId();
      }
    } );

    System.out.println( "id1 = " + id1[0] );
    System.out.println( "id2 = " + id2[0] );

    if(id1[0]!=id2[0]){
      System.out.println("These ID's don't match, even though they were retrieved from the same thread.");
    }

  }

Basically, it gets the ID of the eventqueue thread, waits 5 seconds and then gets the ID again.

For some reason, the ID's won't match. Apparently, the EventQueue was destroyed and recreated. Is this normal behavior? Is this documented somewhere? Is it a bug? Even if it was a different instance, shouldn't it have the same ID?

If I don't perform the Thread.sleep, the ID's will match.

My other question is: How can I get around this? I'm using an object that can only be accessed on the creating thread. If this happens to be the eventqueue (which it doesn't necessarily have to be) I have to be able to check if it is still the eventqueue.


回答1:


That AWT Event Dispatch Thread may be shut down when it's no longer needed (this page describes both the spectification and the behaviour of the actual implementation in JDK 7).

This seems to happen here: You use the system EventQueue to handle one event. Then nothing needs it any more (no AWT/Swing components, ...). After some time it is shut down.

Then, when you use the EventQueue again another thread is started to take over that role.

So what happens here is that your Runnable.run() methods do get executed on two distinct threads. Both threads are "the AWT Event Dispatch Thread", just at different times in the JVMs life-cycle.

Maybe special casing this by using EventQueue.isDispatchThread() would be a possible solution.



来源:https://stackoverflow.com/questions/7190297/eventqueue-inconsistent-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!