I am still learning Lock interface. Till now i didnt see any big difference between ReEntrant lock and Monitor. (Not talking about the ReentrantReadWriteLock which is more flexible and scalable).
Can you give me a hint on the implementation. How did you implement it.
Now i understand Lock and condition a bit ...can you give me hint now ..as i tried but couldnt get the output...
So did you do it with 3 Locks and 3 different condition ....or 1 lock and 3 different conditions ??
Is it possible to implement this logic using Locks & Condition ?
I think providing working code gives better perspective of approach, like we could understand Anuj's way of implementation well when he provided the code. I would request you to share your way of implementation as well. That would help us understanding your approach well.
I would request you to share your way of implementation as well. That would help us understanding your approach well.
Process finished with exit code 0
Chan Ag wrote:
It's open to suggestions and feedback...
I don't like the way of signalling the first signalAll. Let me know if you can think of something better.
Aaron Shawlington wrote:Here's how I did it quickly without using Lock interface, just using synchronize (for an interview I'd probably just use a semaphore with 1 permit instead of my Flag object. Ignore the funny names by the way, I didn't want to take time to think of good names):
Aaron Shawlington wrote:
Henry Wong wrote:
The getFlag() method call isn't quite right. It is possible for the wait() method to return, and the flag isn't in the right state...
Second, I don't like waits on condition variables to be "blind". I mentioned it before many times in these forums, so will do so again... ... Your code should have some sort of state. And check it to confirm that it should call the await() method call. It should also check it after the await() method call to make sure that it is correct before continuing.
Salil Vverma wrote:Hey,
We can implement the solution without using any synchronization construct, by creating threads using callable interface and manipulating them.
In case, you want to refer the code, it is accessible at the following link -
Salil Vverma wrote:Hey Aron,
If we just need to perform similar task in sequence (not in parallel), I do no see any point of doing things using three threads.
But as it is an interview question, I think, interviewer might check any thing, ranging from inter-thread communication, signalling to passing execution result.
That is why I think, while answering,the option of this answer should also be kept open.
I think it's worth bearing in mind, and thanks for that. But if the task was to split processing in parallel threads (to take advantage of multiple cores for example), and
then join the results afterwards, the question wouldn't deliberately arrange the data in a way which needed to be reordered afterwards
I'm pretty sure in a threading interview question the interviewer is NOT trying to see if you're capable of using a for loop to sysout ints from 3 different arrays. That whole part would be pointless,and I think if you solved the question in that way that the interviewer would consider it a fail.
In this case the scenario has clearly taken sequential data, and split it across different threads in such a way that no thread can individually process its data in the same sequential order as it existed before being split. The implication being that the threads must communicate in order to output the data in sequential order. Simply returning the exact same data that was input, and then manually ordering it afterwards is entirely pointless.
Salil Vverma wrote:
I do not see the problem as sequencial data being split by main thread in such a way as therad needs to interact with each other. I see the problem as it is written in question. Where three threads aleady have data and we want data to be processed in specific sequence. The objective is data processing in specific sequence not implementing any specific appraoch.