Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Worker threads in Swing

  1. #1
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Worker threads in Swing

    I'm curious, where and when do all the Swing's worker threads spawn?
    The question raised, when I tried to invoke SwingWorker's execute() method from the main() method of a console application (just for the test purposes) and got no result. I had to use ExecutorService.submit(SwingWorker) method in order to make it execute.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Worker threads in Swing

    Can you make a small program that compiles, executes and shows what happened?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Worker threads in Swing

    Yes, here's the program, who does not run:
    import javax.swing.SwingWorker;
     
    public class Polygon {
     
        public static void main(String[] args) {
            SwingWorker<Void, Void> w = new SwingWorker<Void, Void>() {
     
                @Override
                protected Void doInBackground() throws Exception {
                    System.out.println("I'm started!");
                    return null;
                }
            };
            w.execute();
        }
    }

    Here's the program, who runs.
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import javax.swing.SwingWorker;
     
    public class Polygon {
     
        public static void main(String[] args) {
            SwingWorker<Void, Void> w = new SwingWorker<Void, Void>() {
     
                @Override
                protected Void doInBackground() throws Exception {
                    System.out.println("I'm started!");
                    return null;
                }
            };
     
            ExecutorService e = Executors.newSingleThreadExecutor();
            e.submit(w);
     
            e.shutdown();
        }
    }

    By the way, why NetBeans goes mad when I try to use diamond notation for SwingWorker declaration?
    Here's the Exception thrown:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
    required: javax.swing.SwingWorker<java.lang.Void,java.lang.V oid>
    found: <anonymous javax.swing.SwingWorker<T,V>>
    at polygon.Polygon.main(Polygon.java:10)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Worker threads in Swing

    It executed the doInBackground() when I added this after the execute()
            Thread.sleep(150); //<<<<<<<<<< let above execute
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Worker threads in Swing

    Hmmm, why's that so? Does that mean, that all the threads terminate when the main method returns?
    And why, for example, this code executes the thread without sleeping?
        public static void main(String[] args) throws InterruptedException {
            new Thread(new Runnable(){
     
                @Override
                public void run() {
                    System.out.println("I'm started!");
                }
     
            }).start();
        }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Worker threads in Swing

    Read the API doc for the Thread class and its setDaemon() method.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Worker threads in Swing

    Oh, I see now... And how can I access the currently available worker threads and, perhaps, set some of them not to be a daemon? Where do they spawn and how are they accessed?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Worker threads in Swing

    I don't know if or how to access those threads.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Worker threads in Swing

    I see... Thank you very much, Norm! Those stuff about daemon was really new for me. I'm glad that I understand threads a bit better after your explanations.

Similar Threads

  1. Calculate total earning of a worker
    By Shalom in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 14th, 2012, 06:41 PM
  2. Problem with threads
    By Spidey1980 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 20th, 2011, 09:04 AM
  3. Calling worker methods in JSF.
    By newbie in forum Web Frameworks
    Replies: 0
    Last Post: March 18th, 2011, 02:21 PM
  4. threads
    By crazed8s in forum Threads
    Replies: 2
    Last Post: December 14th, 2010, 05:33 AM

Tags for this Thread