EDT是什么时候开始的?
我猜是"一些SwingComponent. 可见的(真实)" 做的这个把戏, 但我不确定。
谢谢!
EDT是什么时候开始的?
我猜是"一些SwingComponent. 可见的(真实)" 做的这个把戏, 但我不确定。
谢谢!
Swing 的内部操作是 JVM 特有的。 不同的 JVM 启动事件调度线索( EDT ), 依据不同的标准 。 通常 :
EDT从收到第一个
AWTEvent
开始。
下面的堆叠轨迹再次确认此点。 例如, 以下的main
方法 。
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
}
在上述例子中,负责启动电子数据交换的代码线是 < code > frame. set Veabilable( true); 。
上述main
方法在两个不同的JVMs上实施。 在 EventQuuee.init DispatchThread
放置了一个断点。 当中断点被击中时,会发现以下的堆叠痕迹 。
在 AWT-AppKit
线索上使用 Mac s JDK:
EventQueue.initDispatchThread() line: 906
EventQueue.wakeup(boolean) line: 1109
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25
Method.invoke(Object, Object...) line: 597
SunToolkit.wakeupEventQueue(EventQueue, boolean) line: 348
PostEventQueue.postEvent(AWTEvent) line: 2137
SunToolkit.postEvent(AppContext, AWTEvent) line: 583
SunToolkit.executeOnEventHandlerThread(PeerEvent) line: 654
SunToolkit.executeOnEventHandlerThread(Object, Runnable) line: 631
EventFactoryProxy.windowMoved(CWindow) line: 89
在 main
线索上的窗口使用 Oracle s JDK:
java.awt.EventQueue.initDispatchThread() line: 861
java.awt.EventQueue.postEventPrivate(java.awt.AWTEvent) line: 199
java.awt.EventQueue.postEvent(java.awt.AWTEvent) line: 180
javax.swing.RepaintManager.scheduleProcessingRunnable(sun.awt.AppContext) line: 1369
javax.swing.RepaintManager.nativeAddDirtyRegion(sun.awt.AppContext, java.awt.Container, int, int, int, int) line: 548
javax.swing.SwingPaintEventDispatcher.createPaintEvent(java.awt.Component, int, int, int, int) line: 45
sun.awt.windows.WFramePeer(sun.awt.windows.WComponentPeer).postPaintIfNecessary(int, int, int, int) line: 741
sun.awt.windows.WFramePeer(sun.awt.windows.WComponentPeer).handlePaint(int, int, int, int) line: 736
sun.java2d.d3d.D3DScreenUpdateManager.repaintPeerTarget(sun.awt.windows.WComponentPeer) line: 274
sun.java2d.d3d.D3DScreenUpdateManager.createScreenSurface(sun.awt.Win32GraphicsConfig, sun.awt.windows.WComponentPeer, int, boolean) line: 175
...
sun.awt.windows.WToolkit.createFrame(java.awt.Frame) line: 383
javax.swing.JFrame(java.awt.Frame).addNotify() line: 460
javax.swing.JFrame(java.awt.Window).show() line: 859
javax.swing.JFrame(java.awt.Component).show(boolean) line: 1584
javax.swing.JFrame(java.awt.Component).setVisible(boolean) line: 1536
javax.swing.JFrame(java.awt.Window).setVisible(boolean) line: 842
Example.main(java.lang.String[]) line: 113
在Mac上,调用PostEventQueue.postEvent(AWTEvent)
。同样,在Windows上,调用java.awt.EventQueue.postEvent(java.awt.AWTEvent)
。两者最终都调用 EventQuue.init DispatchThread
。
例如,考虑以下main
方法:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
System.out.println("Start!");
}
});
}
使用 main
线索上的 Mac s JDK:
EventQueue.initDispatchThread() line: 906 [local variables unavailable]
EventQueue.postEventPrivate(AWTEvent) line: 227
EventQueue.postEvent(AWTEvent) line: 208
EventQueue.invokeLater(Runnable) line: 1048
SwingUtilities.invokeLater(Runnable) line: 1267
Example.main(String[]) line: 31
在 main
线索上的窗口使用 Oracle s JDK:
java.awt.EventQueue.initDispatchThread() line: 861
java.awt.EventQueue.postEventPrivate(java.awt.AWTEvent) line: 199
java.awt.EventQueue.postEvent(java.awt.AWTEvent) line: 180
java.awt.EventQueue.invokeLater(java.lang.Runnable) line: 999
javax.swing.SwingUtilities.invokeLater(java.lang.Runnable) line: 1267
swindUtities.invokeLater
的呼叫负责启动 EDT。 这里再次呼叫 EventQuue. post Event (AWTEvent)
。
不只是调用 somes SwingComponent. set Veable( true)
来启动 EDT。 例如,执行以下 main
方法不会创建 AWT-Event-Queue-0
线 :
public static void main(String[] args) {
JLabel label = new JLabel();
label.setVisible(true);
}
当然,网上有许多关于特殊和差别待遇的在线资源。
任何AWT / Swing 集装箱从EDT开始使用,不安全的方式
Intial Thread 是最安全的方式
如果在目前的 EDT 中所有事件都完成, 那么是DisapatchThread 返回假
如果DissatchTread返回假的, 那么任何摇线安全方法 都可以活过EDT, 最安全的方法就是从引用( ) 中引用最安全的方法 。
测试的代码代码
import java.awt.Color;
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.getContentPane().setBackground(Color.red);
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;
}
}
你们是对的,EDT不是在启动时直接启动的。我做了一些调试,这就是我找到的:
事件调度线索在某个部件请求访问事件队列时启动, 只需调用 工具包. getEventQuue () 即可启动 。 这可以在调用 subject.show () () (与 subject. set Veabilable () 相同) 时完成, 但还有其他电话可以触发此初始化, 如 subp. repaint () 。 一旦引用了事件队列, 可以在它上添加一个任务, 使用“ 事件队列 ”. postEvent () 。 如果 EDT 存在, 并且如果它没有以 Init DispatchThread () 创建它, 此方法可以检查 。
防止它开始的唯一办法是用“-Djava.awt.headless= true”的旗帜启动无头模式的JVM(它共同禁用了 AWT ) 。 但是这基本上是你唯一可以与之进行低层次互动的。
元件的可视方法应该总是在 EDT 上引用( 就像您对 Swing / AWT 组件所做的任何其它修改一样 ) 。 您通过告诉 Java 执行 EDT 代码来使用 EDT 。 最简单的方法就是使用 SwingUplities. invokeLater () 。 这安排了您从 EDT 上执行的线索( 您的可运行执行程序) 。 这是您作为开发者应该与 EDT 进行的唯一一种互动。 您不应该像 中止或中止线一样, 与 EDT 有任何低级别的互动 。
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...