Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in Multi threading

 
Muhammad Abdul Arif
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.... ]

 
Muhammad Abdul Arif
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic