aspose file tools*
The moose likes Threads and Synchronization and the fly likes Ordered processing - Inter-thread Communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Ordered processing - Inter-thread Communication" Watch "Ordered processing - Inter-thread Communication" New topic
Author

Ordered processing - Inter-thread Communication

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 838
    
  14
Greetings,

Just recently I was working on the following problem, posted by someone else on here.

http://www.coderanch.com/t/619139/threads/java/Thread-Implementation-asked-interview

To summarize the problem statement, we have three threads, and three int arrays of same dimension. We have to use inter thread communication to print the elements of all three arrays as per some ordering scheme. We have to print the nth element of each array before we can move to ( n+1)th element of any of these arrays. There is an order in which we print the nth element of each array. Like for example, if the arrays are as follows--
Array1 = { 1,2,3}
Array2 = { 4,5,6}
Array3 = {7, 8, 9}

and if the print order is Array1, Array3, and Array2 elements respectively, we should get the following output.
1
7
4
2
8
5
3
9
6

I implemented this using ReentrantLock and Condition variable as follows.


I got the following output as expected.
1
3
2
4
6
5
7
9
8

It works correctly ( so far as I could see ) and according to my analysis it seems there is no race condition here that I have overlooked. I also tested it by introducing delays after every instruction the run method executes. The output was same. As per my analysis of how it should function, I see no race condition. But would you say the same?

Also, is there a better way to handle the signalling of the first signal to start the processing. I have to make sure that the three threads are in the wait queues before the first signal is sent, or the first notification could be missed. So I have used lock.getWaitQueueLength(<condition>) method. But to test if the threads are in the wait queues, main has to first acquire the lock, then do the test and since it is possible that main acquires the lock even before the three threads are in the wait queues, it needs to also unlock and then reacquire the lock so it can check again if the three threads are in the wait queue. Is there a better way to do this?

Also would you suggest me to use some other synchronization idiom, not the locks and conditions?

I wanted to post this question long time back, but someone else was working on it then and I thought they'd like to solve it first. But it seems now it's quite ok of me to post this question.
Please let me know if I need provide some more details.

Thanks in advance.
Chan.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3934
    
  17

Chan Ag wrote:As per my analysis of how it should function, I see no race condition. But would you say the same?
I don't see a race condition. The three threads aren't accessing the same data, so it is hard for there to be any chance of inappropriate states. But, in any event, it looks like you have the signalling correct so there will only be one (and the correct) thread active at a time.

Also, is there a better way to handle the signalling of the first signal to start the processing. I have to make sure that the three threads are in the wait queues before the first signal is sent, or the first notification could be missed.
I would consider a CountDownLatch. Each working thread takes the lock, counts down by one, then awaits its signal. The main thread waits for the count to reach zero, then gets the lock signals the first condition.

Also would you suggest me to use some other synchronization idiom, not the locks and conditions?
This is completely linear and serialized behavior: the optimal solution is to use a single thread: the code will be simpler to write, simpler to read, and faster to execute. There is not much point in trying to get too crafty in the solution because it is meaningless and not really applicable to real-world solutions (except as an example of situations where Threads should not be used). That isn't to say that you don't get real-world situations where tasks need to wait and be ordered, but the circumstances will be significantly different (such as sharing data, transferring data, also have parallel components, etc...) and require different strategies.

Steve
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 838
    
  14
Thanks so much, Steve, for helping me.

I will try the CountDownLatch sometime later. Yeah, also a big thanks for the advice on not to spend more time on this problem statement. So now I can get back to the other things that I was working on, before this one came up, without a thought at the back of my mind that I left this one incomplete or that I could have tried the alternatives.

Thank you,
Chan.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ordered processing - Inter-thread Communication
 
Similar Threads
Denny's DVD ReservationsManager - signal()
Thread Implementation Question asked at an interview
How can i call my asynchronous task via synchronous method ?
Question about working of ReentrantLock