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 ??
Regards
Salil Verma
Regards
Salil Verma
Is it possible to implement this logic using Locks & Condition ?
Regards
Salil Verma
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.
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 12.app/bin" -Dfile.encoding=UTF-8 -classpath "/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/deploy.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/dt.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/javaws.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/jce.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/jconsole.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/management-agent.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/plugin.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/sa-jdi.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/charsets.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsse.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/ui.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/apple_provider.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/dnsns.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/localedata.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunjce_provider.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunpkcs11.jar:/Users/aaronshaw/Downloads/Threads/out/production/Threads:/Applications/IntelliJ IDEA 12.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
>1
>2
>3
>4
>5
>6
>7
>8
>9
Done.
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.
Regards
Salil Verma
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 -
http://jyotisalil.blogspot.in/2013/09/inter-thread-communication-using-wait_19.html#CallableCode
Regards
Salil Verma
Regards
Salil Verma
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.
Regards
Salil Verma
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |