| Author |
progressbar in real time and streams
|
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
Hello, I wish to increment a progress bar in real time (file upload). If i use a BufferedOutputStream it appears to move too quick. is it that the data is being written to the stream and the thread moves on. I tried OutputStream which appears realistic. Is this the correct way to implement this? Thanks
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8265
|
|
|
I'm not sure what you mean by "realistic". BufferedOutputStream is probably going to be faster than a plain OutputStream. BufferedOutputStream has a 8k buffer, so if your file is less than that, it will be REAL fast.
|
"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
|
 |
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
|
i would like the progress bar to increment at the same speed as the write to disk. If the BufferedOutputStream buffers the data and writes it later then my progressbar reflects the speed with which data is transfered to a buffer and not to the disk. And because I wait for a response indicating the number of bytes which were written to disk there is a lag between the progressbar at 100% and the actual fact of the data being written to disk. Is this correct? And then should OutputStream remedy this symptom?
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8265
|
|
i would like the progress bar to increment at the same speed as the write to disk.
It probably is. Using BufferedOutputStream should be faster than a plain OutputStream. BufferedOutputStream may have some data in its buffer until you flush and close it, but your code to do that shouldn't be far from the code that's doing the copy, right?
|
 |
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
Thank yo for your response. Is it possible that the thread which i am using to write the file to either bos or os is manipulating the cpu to the point where the thread used to update the progress bar cannot access the cpu?
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8265
|
|
You have that backwards. The OS and JVM collaborate to determine which thread gets the CPU at any time. It's possible to have one thread freeze another thread out if they are of equal priority. Have a look at our FAQ, JProgressBarDoesntUpdate
|
 |
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
The example you provided illustrates the behavior I am observing and also a few possible solutions. Thank you.
|
 |
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
I am using two threads. One writes data to an OutpputStream and the other updates a progress bar. The behavior I am observing is such that the file write loop is executed until all data has been transferred and then it waits for a response for another few seconds. All this time (4-5 seconds) the progress bar thread does not wake up. The progress bar thread is programmed to wake up every second. From what I have read it is possible that the jvm and os are not allowing the progress bar thread cpu access, given both threads have the same priority. Is this correct? Also I notice that the file data is written at and then there is a lag waiting for a response. Is it possible that some sort of cache is happening despite the fact that the thread is writing directly to the OutputStream class.
|
 |
 |
|
|
subject: progressbar in real time and streams
|
|
|