Results 1 to 6 of 6
  1. #1
    _SAS is offline Member
    Join Date
    Jan 2010
    Location
    Russia
    Posts
    9
    Rep Power
    0

    Default StringWorker different behaviour

    Hello.
    First I'd like to tell you what I'm trying to do. I have a tabbed GUI program to download images. Every tab must have its own "downloading thread" independent from other tabs. To simplify multithreaded programming I use SwingWorker threads. I've subclassed SwingWorker and create an instance of it for every tab. Then there's a Swing Timer for every tab which periodically starts the download thread.
    Here's the code for starting a thread:
    Java Code:
        public void startThread() {
            if (running)
                return;
            if (url != null && pathOk) {
                timer.start();
                running = true;
                startbtn.setText("Stop");
                startbtn.setActionCommand("stop");
                refresh();
            }
        }
    
        private void refresh() {
            // here I fill "strings" with some data
            loader = new Loader(strings); // Loader is a subclass of SwingWorker
            loader.execute();
        }
    Here's the code invoked every time the timer ticks:
    Java Code:
        public void processTimer() {
            if (loader != null) {
                if (loader.isDone())
                    refresh();
            } else
                refresh();
        }
    Sometimes Loader's doInBackground() method takes longer that the timer interval, and I don't want to start another thread when the previous one is not finished yet, so this code seems right to me.

    And here is the problem. I tried to run this program with NetBeans' "Debug" to look at the threads. I have two machines. The first one runs Windows 7, the second one Windows XP, both have JDK 1.6.
    On Windows 7 when I create multiple tabs and start multiple SwingWorkers I see only one "SwingWorker-pool-1-thread-1" thread in the debugger. The tasks which are assigned to SwingWorkers are performed one after another: first all the pictures in one tab are downloaded, and after that the pictures in another tab are downloaded. So basically there's only one SwingWorker thread for all the background tasks. On the other hand, on WinXP machine, SwingWorker threads are created one after another at the timer ticks, and they all are shown as "running". I think that I use SwingWorkers properly since the execution always returns from their done() method.
    So here are the questions I have.
    1) Which behaviour for SwingWorkers is normal?
    2) Should I explicitly stop executing at the end of doInBackground() somehow?
    3) How can I control the allocation of thread pools for SwingWorkers?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

  3. #3
    _SAS is offline Member
    Join Date
    Jan 2010
    Location
    Russia
    Posts
    9
    Rep Power
    0

    Default

    So, all I need to do is update my JDK? Thank you very much for your reply! I was afraid there's something wrong with my code and I couldn't find a mistake.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    So, all I need to do is update my JDK?
    Sheesh, why do some people find it so difficult to click a link and read the linked page?

    db

  5. #5
    _SAS is offline Member
    Join Date
    Jan 2010
    Location
    Russia
    Posts
    9
    Rep Power
    0

    Default

    Sorry to confuse you, I didn't want to put it as a question, I probably hit that "?" by mistake. I read all your links and found that that bug was fixed in b21 update.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

Similar Threads

  1. Strange behaviour
    By imadabh in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-11-2011, 03:31 PM
  2. Strange JVM behaviour
    By pjpr in forum Advanced Java
    Replies: 13
    Last Post: 01-03-2011, 07:39 PM
  3. BoxLayout Behaviour
    By PetalumaBoy in forum AWT / Swing
    Replies: 4
    Last Post: 06-10-2009, 01:27 PM
  4. AffinedTransform strange behaviour
    By Echilon in forum AWT / Swing
    Replies: 3
    Last Post: 12-11-2008, 09:58 AM
  5. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 09:23 PM

Posting Permissions

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