The event dispatching thread is used in java to process events from the graphical user interface (GUI).
Java GUI components which redraw themselves or any visual change to the GUI must be updated via EDT.
more on EDT

EDT Java Singleton

All user interface manipulation must be done when on the event dispatch thread, so using this class is a good way to safely manipulate UI components.

The following code works fine for multithreaded access to the getInstance() method.

Here you see a code snippet – you may use any of this code freely as long as you reference

http://sshadmincontrol.com  somewhere in your code.

 /* MonkeyTool
  * Copyright (C) 2012, sshadmincontrol.com
  *
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License
  * as published by the Free Software Foundation; either version 2 of
  * the License, or (at your option) any later version.
  *
  * This code is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  * GNU Library General Public License for more details.
  *
  */
  public class SwingEDTManager {
 
	private static SwingEDTManager INSTANCE;
 
	/**
	 * Provide a default Private constructor constructor is private
	 */
	private SwingEDTManager() {
		super();
	}
 
	/**
	 * @return the instance
	 */
	public static SwingEDTManager getInstance() {
		if (null == INSTANCE) {
			// double-checked locking
			synchronized (SwingEDTManager.class) {
				if (null == INSTANCE) {
					INSTANCE = new SwingEDTManager();
				}
			}
		}
		return INSTANCE;
	}
 
	/**
	 * Returns true if the current thread is an AWT event dispatching thread.
	 *
	 * @return
	 */
	public boolean isInEDT() {
 
		return SwingUtilities.isEventDispatchThread();
	}
 
	/**
	 * asynchronous code execution
	 *
	 * @param runnable
	 */
	public void invokeLaterInDispatchThreadIfNeeded(Runnable runnable) {
 
		if (isInEDT()) {
 
			runnable.run();
 
		} else {
 
			SwingUtilities.invokeLater(runnable);
		}
	}
 
	/**
	 * synchronous code execution
	 *
	 * @param runnable
	 */
	public void invokeAndWaitInDispatchThreadifNeeded(Runnable runnable) {
 
		try {
			if (isInEDT()) {
 
				runnable.run();
 
			} else {
 
				SwingUtilities.invokeAndWait(runnable);
			}
 
		} catch (Exception e) {
 
		}
 
	}
 
	/**
	 * synchronous code execution
	 *
	 * @param runnable
	 */
	public static void invokeAndWaitInDispatchThreads(Runnable runnable) {
 
		try {
 
			SwingUtilities.invokeAndWait(runnable);
 
		} catch (Exception e) {
 
		}
 
	}
 
}

Swing event-dispatching thread

Also don’t forget to try out the monkey tool for free and control your servers remotely through google talk; it’s a great time saver.

Go Back to the tutorial menu page.