Does SwingUtilities.invokeLater() keep order with threads?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Does SwingUtilities.invokeLater() keep order with threads?

Hybrid View

  1. #1
    Join Date
    Sep 2008
    Posts
    2

    Does SwingUtilities.invokeLater() keep order with threads?

    Hi there,

    I think I have a threading issue but it all depends on how invokeLater() works. From my understanding invokeLater() will place each thread on a queue and be handled. The question is does it handle it in order? For example, if I put thread1 on the queue first, does it gaturnetee thread1 will be served up first, or could thread2 be called before thread1?



    Code:
    public static void main(String args[]) {
    		System.out.println("BEFORE RUN");
    		
    		
    		SwingUtilities.invokeLater(
    	            new Runnable()
    	            {
    	                @Override
    	                public void run() {
    	                	try {
    	                		System.out.println("SLEEPING...");
    							Thread.sleep(10000);
    						} catch (InterruptedException e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    						}
    	                	System.out.println("TEST1");
    	                }
    	            });
    		
    		SwingUtilities.invokeLater(
    	            new Runnable()
    	            {
    	                @Override
    	                public void run() {
    	                	System.out.println("TEST2");
    	                }
    	            });
    }

    I keeping getting the same output:
    BEFORE RUN
    SLEEPING...
    TEST1
    TEST2


    Thanks

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Does SwingUtilities.invokeLater() keep order with threads?

    invokeLater() places the Runnable object (not the thread) on the Event Dispatch Queue. The Event Dispatch Thread (EDT) takes Runnable objects off this queue in FIFO order and runs them.

    The output is as it is because both runnable objects are run by the same thread (EDT) and so is doesn't matter how long a delay you put in the first object, the second won't run until the first has completed.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center