Doubt about multithreading
Hello, everybody!
I'm writing a little desktop application to make backups. The application
consists of a list of backup items, each item with source and destination, which
the user can configure. I want to make the backups sequentially, so when one
finishes, another one starts. I also want to make the backups in a separate
thread, using the SwingWorker class. But I was thinking with myself: If I am
going to use a separate thread to make the backup, why not make all the backup
items at the same time, scheduling a separate thread for each one? Wouldn't it
be faster? That's my doubt, as I know that when multiple threads are running in
the same application they are not really running at the same time, but using
time-slice controlled by the processor. So, how would Java react in a machine
with:
- one processor
- more than one processor (would Java divide the work between processors,
making the backup run faster?)
- a processor with the dual core technology (would it be faster here too?)
What I really need to know is that if I really gain something in time if I
schedule a thread for each backup item for the backups to execute at the same
time of if I would better stick with one backup item at a time.
Thank you.
Marcos
> What I really need to know is that if I really gain
> something in time if I
> schedule a thread for each backup item for the
> backups to execute at the same
> time of if I would better stick with one backup item
> at a time.
It depends on how many disks you have. Many "concurrent" read/writes on one disk will kill your performance.
Kaj
> - one processor
> - more than one processor (would Java divide the work
> between processors, making the backup run faster?)
I think that's the OS's task, but yes, that would happen.
> a processor with the dual core technology (would it be faster here too?)
Yes.
In both cases: unless you're using shared devices like the same hard disk. Those don't work in parallel.
> > - one processor
> > - more than one processor (would Java divide the
> work
> > between processors, making the backup run
> faster?)
>
> I think that's the OS's task, but yes, that would
> happen.
Yes, this is a OS's task, thank you for the correction
Marcos
> So, are you saying that if I'm writing to the same
> disk I will not gain performance, so I would better
> stick with one thread at a time?
You might actually lose performance, because of increased head repositioning: instead of writing continuously at one place, you'd write here, then you'd write there, then here again, then there again... you might also "outsmart" the disk caches that way.
Unless the writing process blocks somehow, there's no time you could save.
> You might actually lose performance, because of
> increased head repositioning: instead of writing
> continuously at one place, you'd write here, then
> you'd write there, then here again, then there
> again... you might also "outsmart" the disk caches
> that way.
>
> Unless the writing process blocks somehow, there's no
> time you could save.
Yes, that's make a lot of sense. Thank you for the answer.
Marcos