wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Thread Implementation Question asked at an interview Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Implementation Question asked at an interview" Watch "Thread Implementation Question asked at an interview" New topic
Author

Thread Implementation Question asked at an interview

Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

In the solution you linked to there is no guarantee at all with regards to sequence. All the solution that you linked to does, is to say "here you go threads - I've divided up the work into 3 parts (one each), and when you're all done let me know and i'll reassemble the data in the correct order".


In my program main thread says, hey other threads, we need to perform the tasks (printing the value in this case) in sequence and in any case, we would not be taking the benefit of parallelism so you guys pass your tasks to me. I will take the responsibility of sequencing and performing the task.

That is why the sub threads are not doing any thing but passing their tasks to main thread.


Regards
Salil Verma
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey,

Today I solved the problem using locks and condition. The link of solution is as mentioned below, if you guys wish to see the approach.
http://jyotisalil.blogspot.in/2013/09/inter-thread-communication-using-wait_19.html#LockConditionSequencialLatchCode

anuj guptaa
Greenhorn

Joined: Aug 05, 2013
Posts: 26
Salil Vverma wrote:Anuj : We can implement the solution with lock and condition but I think, we shall have to add unnecessary complexity to make sure that no signal gets missed in that solution.


Hi Salil,

Thanks for your help. I am not sure what you mean by the Missed signal , i think its on a bigger context than this problem.

I also tried solving the same problem using Lock and condition and it works fine. Its on the same line as you did with the
Intrinsic lock and wait/notify solution. Please see the solution below.

The good thing about the Lock/condition solution is that we just used one Extrinsic lock instead of 3 Locks in the Intrinsic solution.
Also the Inter-Thread communication in the Lock/Condition solution is better and more advance. But this solution is still prone to deadlock on a duo-core processor ,so i had to use the Thread.sleep().



anuj guptaa
Greenhorn

Joined: Aug 05, 2013
Posts: 26
Salil Vverma wrote:Hey Anuj,

I tried the solution is quad code processor and found it working fine. But definitely the code is prone to dead lock if first signal from main to first thread gets missed. The chances of single getting missed will increase in a single core processor machine.
The problem can be solved using Semaphore. The complete code can be accessed from the below link -

http://jyotisalil.blogspot.in/2013/09/inter-thread-communication-using-wait_19.html#SemaphoreCode



Hey Salil,

The solution using the wait/notify and Seamphore are so similar but wait/notify is prone to deadlock while Seamphore is not , how come ? Any clue. I think its the internal implementation for these 2 that makes the difference.

Anuj
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

I am not sure what you mean by the Missed signal , i think its on a bigger context than this problem


You are right, missed signal is a concept where notify might get called before corresponding wait method. In that case the effect of notify gets missed and wait method does not get resumed.

The solution using the wait/notify and Seamphore are so similar but wait/notify is prone to deadlock while Seamphore is not , how come ? Any clue. I think its the internal implementation for these 2 that makes the difference.


You are right, It is internal implementation of semaphore that make sure that signal does not gets missed.

The solution you have implemented by using 'Thread.sleep(1000);' just reduces the chances of signal getting missed by stopping main thread for 1 second. but does not eliminate it. This also add additional delay in the program.

That is why I would recommend to implement solution without using sleep method. You can refer the samples in below mentioned links and think how you can improve your lock - condition code further to work fine

Wait -Nofity solution - http://jyotisalil.blogspot.in/2013/09/inter-thread-communication-using-wait_19.html#WaitNotifySequencialLatchCode
Lock - Condition solution - http://jyotisalil.blogspot.in/2013/09/inter-thread-communication-using-wait_19.html#LockConditionSequencialLatchCode

Regards
Salil Verma


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Implementation Question asked at an interview
 
Similar Threads
Array
OK, so how do I get the source from a document?
Output even more unexpected than I was expecting!
Threads and join
Running threads in Sequence