下面列出了java.awt.EventQueue#isDispatchThread() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void loadAnalysis(final URL url) {
Runnable runnable = () -> {
mainFrame.acquireDisplayWait();
try {
Project project = new Project();
project.setGuiCallback(mainFrame.getGuiCallback());
BugLoader.loadBugs(mainFrame, project, url);
project.getSourceFinder(); // force source finder to be
// initialized
mainFrame.updateBugTree();
} finally {
mainFrame.releaseDisplayWait();
}
};
if (EventQueue.isDispatchThread()) {
new Thread(runnable, "Analysis loading thread").start();
} else {
runnable.run();
}
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if (LOGGER.isLoggable(Level.FINE)) {
// Check if MBeanServerConnection call is performed on EDT
if (EventQueue.isDispatchThread()) {
Throwable thrwbl = new Throwable();
LOGGER.log(Level.FINE, createTracedMessage("MBeanServerConnection call " + // NOI18N
"performed on Event Dispatch Thread!", thrwbl)); // NOI18N
}
}
// Invoke MBeanServerConnection call
try {
return method.invoke(conn, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
public List<RepositoryConnection> getRecentUrls() {
Preferences prefs = getPreferences();
List<String> urls = Utils.getStringList(prefs, KEY_RECENT_URL);
List<RepositoryConnection> ret = new ArrayList<RepositoryConnection>(urls.size());
List<RepositoryConnection> withPassword = new LinkedList<RepositoryConnection>();
for (String urlString : urls) {
RepositoryConnection rc = RepositoryConnection.parse(urlString);
if (rc.getPassword() != null || rc.getCertPassword() != null) {
withPassword.add(rc);
} else {
if(getUrlCredentials().containsKey(rc.getUrl())) {
Object[] creds = getUrlCredentials().get(rc.getUrl());
if(creds.length < 3) continue; //skip garbage
rc = new RepositoryConnection(rc.getUrl(), (String)creds[0], (char[])creds[1], rc.getExternalCommand(), rc.getSavePassword(), rc.getCertFile(), (char[])creds[2], rc.getSshPortNumber());
} else if (!EventQueue.isDispatchThread()) {
char[] password = rc.getSavePassword() ? KeyringSupport.read(KEY_PASSWORD, rc.getUrl().toString()) : null;
char[] certPassword = rc.getCertFile().isEmpty() ? null : KeyringSupport.read(KEY_CERT_PASSWORD, rc.getUrl().toString());
rc = new RepositoryConnection(rc.getUrl(), rc.getUsername(), password, rc.getExternalCommand(), rc.getSavePassword(), rc.getCertFile(), certPassword, rc.getSshPortNumber());
}
ret.add(rc);
}
}
// there's an old-style connection with password set
// rewrite these connections with the new version with no password included
if (withPassword.size() > 0) {
for (RepositoryConnection conn : withPassword) {
insertRecentUrl(conn);
}
return getRecentUrls();
}
return ret;
}
@Override
public void engineRemoved(final ScriptEngineWrapper scriptEngineWrapper) {
if (getView() == null) {
return;
}
if (!EventQueue.isDispatchThread()) {
try {
EventQueue.invokeAndWait(
new Runnable() {
@Override
public void run() {
engineRemoved(scriptEngineWrapper);
}
});
} catch (InvocationTargetException | InterruptedException e) {
LOGGER.error("Failed to update the UI:", e);
}
return;
}
ScriptNode node = getSelectedNode();
if (node != null && node.getUserObject() instanceof ScriptWrapper) {
ScriptWrapper scriptWrapper = (ScriptWrapper) node.getUserObject();
if (ExtensionScript.hasSameScriptEngine(scriptWrapper, scriptEngineWrapper)) {
displayType(scriptWrapper.getType());
}
}
}
public CloneDocumentAction() {
putValue(NAME, NbBundle.getMessage(CloneDocumentAction.class, "CTL_CloneDocumentAction"));
TopComponent.getRegistry().addPropertyChangeListener(
WeakListeners.propertyChange(this, TopComponent.getRegistry()));
// #126355 - may be called outside dispatch thread
if (EventQueue.isDispatchThread()) {
updateEnabled();
} else {
SwingUtilities.invokeLater(this);
}
}
void showDiff(final RepositoryRevision.Event revision) {
Runnable r = new Runnable() {
@Override
public void run() {
tbDiff.setSelected(true);
refreshComponents(true);
diffView.select(revision);
}
};
if(EventQueue.isDispatchThread()) {
r.run();
} else {
EventQueue.invokeLater(r);
}
}
static TimerTask newDebugTask () {
return new TimerTask() {
public void run () {
if (!EventQueue.isDispatchThread()) {
EventQueue.invokeLater(this);
return;
}
for (TableLayout layout : debugLayouts)
layout.drawDebug();
timer.schedule(newDebugTask(), 250);
}
};
}
protected void showDiffError (final String s) {
Runnable inAWT = new Runnable() {
@Override
public void run() {
setBottomComponent(new NoContentPanel(s));
}
};
if (EventQueue.isDispatchThread()) {
inAWT.run();
} else {
EventQueue.invokeLater(inAWT);
}
}
private NBGitConfig getNetbeansConfig (File root) {
if (root == null) {
return null;
}
if (nbConfig == null) {
nbConfig = new NBGitConfig(root);
}
if (!EventQueue.isDispatchThread()) {
nbConfig.refresh();
}
return nbConfig;
}
@Override
public void breakpointAdded (final Breakpoint breakpoint) {
final boolean[] started = new boolean[] { false };
if (!EventQueue.isDispatchThread() && debugger.accessLock.readLock().tryLock()) { // Was already locked or can be easily acquired
try {
createBreakpointImpl (breakpoint);
} finally {
debugger.accessLock.readLock().unlock();
}
return ;
} // Otherwise:
debugger.getRequestProcessor().post(new Runnable() {
@Override
public void run() {
debugger.accessLock.readLock().lock();
try {
synchronized (started) {
started[0] = true;
started.notify();
}
createBreakpointImpl (breakpoint);
} finally {
debugger.accessLock.readLock().unlock();
}
}
});
if (!EventQueue.isDispatchThread()) { // AWT should not wait for debugger.LOCK
synchronized (started) {
if (!started[0]) {
try {
started.wait();
} catch (InterruptedException iex) {}
}
}
}
}
@Override
public void paint(final Graphics g) {
super.paint(g);
if (!EventQueue.isDispatchThread()) {
throw new RuntimeException("Wrong thread");
}
test();
}
/**
* Updates the content of this {@code TopComponent}.
*/
final void update() {
if (EventQueue.isDispatchThread()) {
PageModel pageModel = PageInspectorImpl.getDefault().getPage();
if (currentPanel != null) {
if (pageModel == currentPanel.getPageModel()) {
return;
} else {
currentPanel.dispose();
}
}
removeAll();
currentPanel = new KnockoutPanel((WebKitPageModel)pageModel);
if (lastKnockoutPageModel != null) {
PageModel knockoutPageModel = lastKnockoutPageModel.get();
if (knockoutPageModel != null && knockoutPageModel == pageModel) {
currentPanel.knockoutUsed(lastKnockoutVersion);
}
}
add(currentPanel);
((KnockoutTCLookup)getLookup()).setPanel(currentPanel);
revalidate();
repaint();
} else {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
update();
}
});
}
}
@Override
protected void setCurrentSubcategory(String subpath) {
EventQueue.isDispatchThread();
super.setCurrentSubcategory(subpath);
if (subpath != null
&& panel != null) {
subpath = FRAMEWORKS_AND_TOOLS_OPTIONS_PATH + "/" + subpath; //NOI18N
AdvancedOption option = id2option.get(subpath);
if (option != null) {
panel.setSelecteOption(option);
}
}
}
void rescan(){
if (javaSrc == null) {
javaSrc = JavaSource.forFileObject(file);
}
if (javaSrc != null){
try{
if(EventQueue.isDispatchThread()) {
wsHintsTask.schedule(100);
} else {
javaSrc.runUserActionTask(new ProblemFinderCompControl(file), true);
}
} catch (IOException e){
}
}
}
static void nodeRename(final Node n, final String newStr) {
// bugfix #21589 don't update name if there is not any change
if (n.getName().equals(newStr)) {
return;
}
if (EventQueue.isDispatchThread() && Boolean.TRUE.equals(n.getValue("slowRename"))) { // NOI18N
RP.post(new Runnable() {
@Override
public void run() {
nodeRename(n, newStr);
}
});
return;
}
try {
n.setName(newStr);
} catch (IllegalArgumentException exc) {
boolean needToAnnotate = Exceptions.findLocalizedMessage(exc) == null;
// annotate new localized message only if there is no localized message yet
if (needToAnnotate) {
String msg = NbBundle.getMessage(
TreeViewCellEditor.class, "RenameFailed", n.getName(), newStr
);
Exceptions.attachLocalizedMessage(exc, msg);
}
Exceptions.printStackTrace(exc);
}
}
static boolean isEventQueue() {
return EventQueue.isDispatchThread();
}
static boolean isEventQueue() {
return EventQueue.isDispatchThread();
}
/**
* @see java.awt.im.InputContext#removeNotify
* @exception NullPointerException when the component is null.
*/
public synchronized void removeNotify(Component component) {
if (component == null) {
throw new NullPointerException();
}
if (inputMethod == null) {
if (component == currentClientComponent) {
currentClientComponent = null;
}
return;
}
// We may or may not get a FOCUS_LOST event for this component,
// so do the deactivation stuff here too.
if (component == awtFocussedComponent) {
focusLost(component, false);
}
if (component == currentClientComponent) {
if (isInputMethodActive) {
// component wasn't the one that had the focus
deactivateInputMethod(false);
}
inputMethod.removeNotify();
if (clientWindowNotificationEnabled && addedClientWindowListeners()) {
removeClientWindowListeners();
}
currentClientComponent = null;
if (inputMethod instanceof InputMethodAdapter) {
((InputMethodAdapter) inputMethod).setClientComponent(null);
}
// removeNotify() can be issued from a thread other than the event dispatch
// thread. In that case, avoid possible deadlock between Component.AWTTreeLock
// and InputMethodContext.compositionAreaHandlerLock by releasing the composition
// area on the event dispatch thread.
if (EventQueue.isDispatchThread()) {
((InputMethodContext)this).releaseCompositionArea();
} else {
EventQueue.invokeLater(new Runnable() {
public void run() {
((InputMethodContext)InputContext.this).releaseCompositionArea();
}
});
}
}
}
protected void performAction(final Node[] activatedNodes) {
if (activatedNodes == null || activatedNodes.length == 0) {
return;
}
Node n = activatedNodes[0]; // we supposed that one node is activated
// for slow FS perform rename out of EDT
if (EventQueue.isDispatchThread() && Boolean.TRUE.equals(n.getValue("slowRename"))) { // NOI18N
RP.post(new Runnable() {
@Override
public void run() {
performAction(activatedNodes);
}
});
return;
}
NotifyDescriptor.InputLine dlg = new NotifyDescriptor.InputLine(
NbBundle.getMessage(RenameAction.class, "CTL_RenameLabel"),
NbBundle.getMessage(RenameAction.class, "CTL_RenameTitle")
);
dlg.setInputText(n.getName());
if (NotifyDescriptor.OK_OPTION.equals(DialogDisplayer.getDefault().notify(dlg))) {
String newname = null;
try {
newname = dlg.getInputText();
if (!newname.equals("")) {
n.setName(dlg.getInputText()); // NOI18N
}
} catch (IllegalArgumentException e) {
// determine if "printStackTrace" and "new annotation" of this exception is needed
boolean needToAnnotate = Exceptions.findLocalizedMessage(e) == null;
// annotate new localized message only if there is no localized message yet
if (needToAnnotate) {
Exceptions.attachLocalizedMessage(e,
NbBundle.getMessage(RenameAction.class,
"MSG_BadFormat",
n.getName(),
newname));
}
Exceptions.printStackTrace(e);
}
}
}
static boolean isEventQueue() {
return EventQueue.isDispatchThread();
}