Results 1 to 8 of 8
  1. #1
    Brad is offline Member
    Join Date
    Feb 2012
    Posts
    21
    Rep Power
    0

    Default Problem with synchonizing my jprogressbar

    I have a task that I am using a jprogressbar on and it never seems to sync up quit right. On some of my longer tasks my progress bar reads 100%, but the task doesn't end for another 15-20 seconds. On some of my shorter tasks the progress bar only gets to about 10% before the task finishes. Code is below....

    Thanks

    Java Code:
    //load repository 
          if(!"failed".equals(command)){
             final Thread runLoad = new Thread(new loadRepo(command));
             runLoad.start();
          
           //update progress bar with the jobs progress 
            SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {  
                @Override  
                protected Void doInBackground() throws Exception {  
                    int i = 0;  
                    while (runLoad.getState() != Thread.State.TERMINATED && i < 100){  
                        try {  
                            synchronized(this) {  
                                wait(1000L);  
                            }  
                              publish((Void[])new Void[1]);  
                           }catch (InterruptedException e){
                               JOptionPane.showMessageDialog(null,
                               "Process interrupted: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                           }  
                    }  
                    return null;  
                }  
      
                @Override
                protected void process(List<Void> chunks) {  
                  svn_ProgressBar.setValue(svn_ProgressBar.getValue() + chunks.size());
                  super.process(chunks);  
                }
                @Override
                public void done() {
                 //Tell progress listener to stop updating progress bar.
                   svn_ProgressBar.setValue(svn_ProgressBar.getMinimum());
                 }
            };  
           worker.execute();     
          }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,019
    Rep Power
    6

    Default Re: Problem with synchonizing my jprogressbar

    I suggest you submit a fully compilable program that demonstrates the problem. Simulate a lengthy task in one thread and show how it is being updated in the other. That way you may discover the problem yourself or at least give us something to work with.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Brad is offline Member
    Join Date
    Feb 2012
    Posts
    21
    Rep Power
    0

    Default Re: Problem with synchonizing my jprogressbar

    The thread runLoad.start(); is my lengthy task. It backs up Subversion repositories that range from 30MB to 3GB. SwingWorker then updates my jprogressbar. I would think that synchronized(this) would synchronize runLoad and my SwingWorker thread, but its not looking that way.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,019
    Rep Power
    6

    Default Re: Problem with synchonizing my jprogressbar

    Are you setting the maximum of the progress bar to the size of the file and then simply updating the total bytes written?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Brad is offline Member
    Join Date
    Feb 2012
    Posts
    21
    Rep Power
    0

    Default Re: Problem with synchonizing my jprogressbar

    Not at all. The only code I have written for my progress bar is what is above. I didn't know that I had to read the file size and bytes written to get my progress bar to update correctly (not that I am opposed to this or anything). I just assumed that I could easily synchronize my progress bar to a running thread and as long as the condition in my while loop was met (runDump.getState() != Thread.State.TERMINATED && i < svn_ProgressBar.getMaximum()) the two would magically end at the same time.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,019
    Rep Power
    6

    Default Re: Problem with synchonizing my jprogressbar

    Try this. Set progressBar maximum to size of file. Then continue to set the value of the progress bar to how many you have transmitted. That way, it will know how to make the bar deterministic. Also, if you setStringPainted(true) it will paint a percentage on the bar.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,019
    Rep Power
    6

    Default Re: Problem with synchonizing my jprogressbar

    One more thing to consider. The maximum value for a JProgressBar is an int. So if you are transmitting 30Gbytes then you will need to scale your value and your update. So if you set your max to 30Mbytes you would simply update the value by one for every 1000 bytes transmitted. The progress bar internals will calculate how much to paint based on current value and maximum value. Even if the bars are different lengths, it all works out.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Brad is offline Member
    Join Date
    Feb 2012
    Posts
    21
    Rep Power
    0

    Default Re: Problem with synchonizing my jprogressbar

    Thanks for the advice. When I get to work in the morning I will try to code this up. This is the first project that I have tried implemented a progress bar. I am hoping for success, but if not I am sure I will end up back here.

Similar Threads

  1. JProgressBar with Input
    By jnaevwa in forum AWT / Swing
    Replies: 8
    Last Post: 07-05-2011, 01:34 PM
  2. Updating JProgressBar
    By suraj.routh in forum AWT / Swing
    Replies: 1
    Last Post: 06-04-2011, 02:57 AM
  3. i try to put JProgressBar i jtabl
    By yontan8888 in forum AWT / Swing
    Replies: 2
    Last Post: 03-01-2011, 08:19 AM
  4. Problem With JProgressBar
    By akssingh400 in forum AWT / Swing
    Replies: 6
    Last Post: 02-24-2011, 07:08 PM
  5. JprogressBar
    By cotarelo in forum AWT / Swing
    Replies: 7
    Last Post: 03-26-2010, 04:13 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
  •