| Author |
Concurrency - entry List -> Multithreads ->results List
|
Gerardo Corro
Greenhorn
Joined: Sep 14, 2007
Posts: 2
|
|
Hi All, I have a very long entry list that has to be processed and based in an analysis algorithm they will be send to either an accepted or rejected list. We need to process this list in concurrency. How can I have all these working in concurrency? I mean, what I don't know how to implement is how I could run all those many threads and make them put their results in the same place (the single acepted or rejected lists). Thnak you...
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
How can I have all these working in concurrency? I mean, what I don't know how to implement is how I could run all those many threads and make them put their results in the same place (the single acepted or rejected lists).
Well, it really depends on whether it is even possible. If each element is completely independent, and can be processed in any order, then it is actually very simple. Just make sure that all three lists are the synchronized versions -- the original list, the accepted list, and the rejected list. Have each thread take (remove) the first element, process it, and place it in one of the result lists. Try not to use the get() method, because then you can't tell if two threads are getting the same element. Also try not to use the iterator either, for the same reason. Use the remove() method. BTW, there are probably many other ways to do this. I am just giving a way to use the lists directly, without directly dealing with synchronization. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
A multi-threading approach will only help if the processes are not CPU bound, that is, if they are waiting on something outside the CPU at some point. Disk IO or HTTP requests to a remote partner are ideal examples of things that wait. While one thread is waiting, another thread can run. But if you're just doing math or encryption or some in-memory algorithm, switching between threads might actually hurt. Does your application use lots of CPU in a single thread mode?
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
Multi-threading can also speed up a CPU-bound task, if there is more than one processor available.
|
"I'm not back." - Bill Harding, Twister
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
You rich guys with multiple processors. I grew up with a half a processor and liked it that way. Thanks for the fix.
|
 |
Mr. C Lamont Gilbert
Ranch Hand
Joined: Oct 05, 2001
Posts: 1170
|
|
Originally posted by Jim Yingst: Multi-threading can also speed up a CPU-bound task, if there is more than one processor available.
And slow it down if there is not.
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
As Stan had already said, yes.
|
 |
 |
|
|
subject: Concurrency - entry List -> Multithreads ->results List
|
|
|