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.