EventQueue ID不一致 [英] EventQueue inconsistent ID's

查看:240
本文介绍了EventQueue ID不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,下面的示例代码显示不一致的行为为EventQueue:

  public static void main [] 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(这些ID不匹配,即使它们是从同一个线程检索的。 );
}

}

基本上,事件队列线程,等待5秒,然后再次获取ID。



由于某些原因,ID将不匹配。显然,EventQueue被销毁和重新创建。这是正常的行为吗?这是记录在某处吗?这是一个错误?即使它是一个不同的实例,它不应该有相同的ID?



如果我不执行Thread.sleep,ID将匹配。 p>

我的其他问题是:我如何解决这个问题?我使用的对象,只能在创建线程上访问。如果这恰巧是事件队列(它不一定是必须),我必须能够检查它是否仍然是事件队列。



Thx提前任何帮助。

解决方案

当AWT事件分派主题可能会在不再需要时被关闭=http://download.oracle.com/javase/7/docs/api/java/awt/doc-files/AWTThreadIssues.html#Autoshutdown =nofollow>此页面描述了和在JDK 7中的实际实现的行为)。



这似乎发生在这里:你使用系统 EventQueue 来处理一个事件。然后没有什么需要它了(没有AWT / Swing组件,...)。



然后,当您使用 EventQueue 时,另一个线程开始执行因此,这里发生的是你的 Runnable.run()方法 do

/ strong>在两个不同的线程上执行 两个线程都是AWT事件分派主题,只是在JVM生命周期的不同时间。



EventQueue .isDispatchThread() 是一个可能的解决方案。


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.

Thx in advance for any help.

解决方案

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.

这篇关于EventQueue ID不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆