Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: JOptionPane blocking the Swing thread...?

  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default JOptionPane blocking the Swing thread...?

    I read somewhere that when a dialog blocks the Swing thread, another thread spawns to continue processing events. I wrote this test program to see exactly what is going on. It seems to show that the original Swing thread is blocked and continues to process events, which obviously can't really be what's happening.

    Actually, I don't care exactly how it works. My concern is this: Is it guaranteed that the thread that created the blocking dialog and the thread that continues to process events in the background are either the same thread, or synchronized on the event queue? Some other threads will be posting Runnables that cause the Swing thread to access some collections, and I want to make sure I don't have to sync those accesses.

    Java Code:
    package test;
    
    import java.awt.Dimension;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class SwingBlocks {
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				JFrame frame = new JFrame();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				JPanel panel = new JPanel();
    				panel.setPreferredSize(new Dimension(640, 480));
    				frame.add(panel);
    				frame.pack();
    				frame.setVisible(true);
    				
    				new Thread(new Runnable() {
    					@Override
    					public void run() {
    						try {
    							while(true) {
    								Thread.sleep(3000);
    								SwingUtilities.invokeLater(new Runnable() {
    									@Override
    									public void run() {
    										System.out.println("Handled by " + Thread.currentThread().getName() + " (" + Thread.currentThread().toString() + ')');									
    									}
    								});
    							}
    						} catch (InterruptedException e) {
    							return;
    						}
    					}					
    				}).start();
    				
    				JOptionPane.showConfirmDialog(frame, "Blocking thread " + Thread.currentThread().getName() + " (" + Thread.currentThread().toString() + ')');
    				System.out.println("Thread " + Thread.currentThread().getName()+ " (" + Thread.currentThread().toString() + ") resuming");
    			}
    		});
    	}	
    }
    Last edited by kjkrum; 10-06-2013 at 10:30 PM.
    Get in the habit of using standard Java naming conventions!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: JOptionPane blocking the Swing thread...?

    The JOptionPane methods show a JDialog, which is a Dialog, and this is from the API for Dialog.show():

    If the dialog is modal and is not already visible, this call will not return until the dialog is hidden by calling hide or dispose. It is permissible to show modal dialogs from the event dispatching thread because the toolkit will ensure that another event pump runs while the one which invoked this method is blocked.
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: JOptionPane blocking the Swing thread...?

    That's the part I already knew. My question is whether that other event pump is guaranteed to either be the same thread as the original event pump, or synchronized with it in some way. It seems like the answer should be yes, but I just want to make sure.
    Get in the habit of using standard Java naming conventions!

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: JOptionPane blocking the Swing thread...?

    I'd recommend taking a look at the Dialog.show() source code, if you're really curious about the nitty gritty. It'll eventually lead you here: SecondaryLoop (Java Platform SE 7 )
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: JOptionPane blocking the Swing thread...?

    Ah, thank you. I was under the mistaken impression that a new thread may be started to run the second event pump. Sounds like that is not the case.

    "When a modal dialog is shown on the event dispatch thread, it enters a new secondary loop. Later, when the dialog is hidden or disposed, it exits the loop . . ."
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Swing GUI Thread Error?
    By dharav in forum New To Java
    Replies: 3
    Last Post: 01-18-2012, 06:02 PM
  2. Thread v swing timer
    By musico in forum New To Java
    Replies: 1
    Last Post: 06-23-2011, 09:51 PM
  3. Prompt for value in swing thread
    By dataphile in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-05-2010, 12:50 AM
  4. best way to signal a thread that's blocking on a socket receive
    By danieru in forum Threads and Synchronization
    Replies: 5
    Last Post: 09-08-2009, 04:47 PM
  5. using javax.swing.JOptionPane in array
    By icedragon770 in forum AWT / Swing
    Replies: 12
    Last Post: 11-10-2008, 08:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •