• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrency - entry List -> Multithreads ->results List

 
Gerardo Corro
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22540
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multi-threading can also speed up a CPU-bound task, if there is more than one processor available.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Stan had already said, yes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!