aspose file tools*
The moose likes Threads and Synchronization and the fly likes Need help in Multi threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Need help in Multi threading" Watch "Need help in Multi threading" New topic
Author

Need help in Multi threading

Muhammad Abdul Arif
Greenhorn

Joined: Feb 04, 2012
Posts: 25
Hi All,

I am a noob when comes to threading. Please help me out in this.
I have a requirement where records from a list are picked up by 4 threads concurrently.
For example a list contains 100 elements. First thread should pick first element from the list, second thread should pick second element from the list and so on. The thread that completes the processing should pick up next element from the list. What is the best way of implementing this.

Thanks
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Use a synchronized index in a static method, or synchronized method.

The list has to be accessible to all threads statically to avoid concurrency issues.

WP
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

William P O'Sullivan wrote:...The list has to be accessible to all threads statically to avoid concurrency issues.


I disagree with the thought that the list should be accessible statically to avoid concurrency issues. Being accessible statically is as likely to introduce concurrency issues as it is to help avoid them -> that is to say, it has no real affect.

I usually prefer Queues to Lists in these situations, since it seems to fit better into the use-case (i.e. each access to the collection should remove the element from the collection, so the next access sees the next element). Note that a LinkedList is a Queue implementation.

And since you are working in a multi-threaded environment, one of the Queues in the java.util.concurrent package should be considered. For example, the ConcurrentLinkedQueue seems a nice choice for your situation (as thus far explained).


[edit] fixed first sentence.


Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18828
    
  40

Muhammad Abdul Arif wrote:Hi All,

I am a noob when comes to threading. Please help me out in this.
I have a requirement where records from a list are picked up by 4 threads concurrently.
For example a list contains 100 elements. First thread should pick first element from the list, second thread should pick second element from the list and so on. The thread that completes the processing should pick up next element from the list. What is the best way of implementing this.


First, I would recommend removing the "first thread should pick first element from the list, second thread should pick second element from the list and so on" requirement. It should be something along the lines of "the next free thread gets the next item to process". Threading is dependent on lots of factors, and it is actually hard to require that the threads work in a perfectly interlaced manner.

Second, if you are willing to give on on the "list" requirement, meaning the List interface, I would recommend a BlockingQueue, as that is specifically designed the producer/consumer use case. In your example, the processing threads will be consumers which fetches elements from the BlockingQueue, and it is the queue's responsibility to be thread safe, and to deal with the edge cases -- such as wait if no elements are available, wait if the queue is full, etc. The producers put items on the queue. The consumers take items from the queue. And it is the queue that takes care of the stuff that allows it to be used from multiple threads.

Henry

[EDIT: Beaten to the answer again. This always happens when I walk away from my computer mid-post.... ]


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Muhammad Abdul Arif
Greenhorn

Joined: Feb 04, 2012
Posts: 25
what will the below code does....in was surfing the net and found this in some site. Can i use ExecutorService for my requirement?


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Have a read of this tutorial: http://docs.oracle.com/javase/tutorial/essential/concurrency/

At the end there is a section on 'high level concurrency' which covers executors (and other apis).

Can you use executors in your code? That depends on your situation - read the tutorial and get an idea of what they do and what they are used for and you should be able to answer that question better than we can.
Muhammad Abdul Arif
Greenhorn

Joined: Feb 04, 2012
Posts: 25
Thanks Steve....the link was very helpful.
I thought of going with ExecutorService for my requirement.
Also i wanted to know how to stop processing of all other threads is one thread execution failed due to some error?

Thanks,
Arif.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

To cancel other threads, you need a way to signal cancellation between threads. You will, at least, have to handle thread interruption at the correct spots, and maybe another signal to indicate your desire to stop processing (so you know that a particular interruption was caused by a desire to stop, and not some other error.) If you handle interruption and signal your desire to stop then you can use the ExecutorService API to cancel all threads.

I generally provide:
1 - a volatile boolean which tells me if I should stop
2 - any loops that take time check that flag plus the Thread.isInterrupted() status.
3 - other strategic parts of code that can be used as a safe place to stop and clean up do the same checks
4 - Handle InterruptedExceptions, part of which will be to check the 'should I stop' flag, and act appropriately

Then you just need methods where one thread can signal other threads they should stop. A central 'Controller' type object which handles the ExecutorService (if you use one) and distributes the tasks to execute is usually a good point to do the signalling (i.e. a task tells the controller it failed and the controller signals other threads they should stop.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help in Multi threading