首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java API“如果不在EDT上运行,则在EDT上运行”

Java API“如果不在EDT上运行,则在EDT上运行”
EN

Stack Overflow用户
提问于 2012-03-21 06:13:37
回答 3查看 1K关注 0票数 4

我只是对我的一些重复代码进行了思考:

代码语言:javascript
复制
Runnable run = new Runnable() {
  @Override
  public void run() {
    // Some EDT code
  }
};

if (!EventQueue.isDispatchThread()) {
  SwingUtilities.invokeAndWait(run);
} else {
  run.run();
}

这并不是非常烦人,但似乎会有一些专有函数为您检查这一点,尽管我还没有找到它。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-24 08:28:40

看起来有一些专有的函数可以帮你检查这个

没有。

票数 3
EN

Stack Overflow用户

发布于 2012-03-21 07:23:18

除了物质外观和感觉,我从来不需要使用invokeAndWaitisDispatchThread / isEventDispatchThread测试,

物质外观在某些特殊情况下需要使用用于JTableJTreeSwingX's TreeTableinvokeAndWait,在这种情况下真的不重要

延迟30秒是“EDT的到期”,然后您可以创建n_Threads,然后每个都可以运行EDT,简单,不需要特别的努力,另一个限制可能是本机操作系统的延迟

输出

代码语言:javascript
复制
run:
                         Time at : 00:00:45
There isn't Live EventQueue.isDispatchThread, why any reason for that 
There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that 

                         Time at : 00:00:45
EventQueue.isDispatchThread
SwingUtilities.isEventDispatchThread

                         Time at : 00:00:45
EventQueue.isDispatchThread
SwingUtilities.isEventDispatchThread

                         Time at : 00:01:15
There isn't Live EventQueue.isDispatchThread, why any reason for that 
There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that 

Push a new event to EDT
                         Time at : 00:01:45
EventQueue.isDispatchThread
SwingUtilities.isEventDispatchThread

                         Time at : 00:02:15
There isn't Live EventQueue.isDispatchThread, why any reason for that 
There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that 

                         Time at : 00:02:45
There isn't Live EventQueue.isDispatchThread, why any reason for that 
There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that 

Push a new event to EDT
                         Time at : 00:03:15
EventQueue.isDispatchThread
SwingUtilities.isEventDispatchThread

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

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

Push a new event to EDT
                         Time at : 00:04:45
EventQueue.isDispatchThread
SwingUtilities.isEventDispatchThread

Terminating this madness
BUILD SUCCESSFUL (total time: 4 minutes 31 seconds)

从…

代码语言:javascript
复制
import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;
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;
    private JFrame frame1 = new JFrame("Frame 1");

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

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

            @Override
            public void run() {
                countCalled++;
                if (countCalled < maxCalled) {
                    if (countCalled % 3 == 0) {
                        SwingUtilities.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                System.out.println("Push a new event to EDT");
                                frame1.repaint();
                                isThereReallyEDT();
                            }
                        });
                    } else {
                        if (this.getExecutionTime() < ALLOWED_TARDINESS) {
                            countRun++;
                            isThereReallyEDT(); // non on EDT
                        }
                    }
                } else {
                    System.out.println("Terminating this madness");
                    System.exit(0);
                }
            }
        };
        periodicMonitor = scheduler.scheduleAtFixedRate(periodic, 0, taskPeriod, TimeUnit.SECONDS);
        periodic.setThreadMonitor(periodicMonitor);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                isThereReallyEDT();
                frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame1.getContentPane().add(new JLabel("Hello in frame 1"));
                frame1.pack();
                frame1.setLocation(100, 100);
                frame1.setVisible(true);
            }
        });
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            Logger.getLogger(IsThereEDT.class.getName()).log(Level.SEVERE, null, ex);
        }
        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();
            }
        });
    }

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

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;
    }
}

你可以做任何事情,改变顺序,方法,冻结Thread.sleep(int),在所有情况下都可以在假设的时刻激活EDT,没有阴影,任何奇迹,调用天堂而不是EDT的错误代码

票数 2
EN

Stack Overflow用户

发布于 2012-03-21 06:24:32

考虑使用

http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html

http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html

而不是。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9795834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档