Hi Raja,you are doing plenty of things erroneously in your code,the first step is to understand the problem-
.the Boolean object referenced by available is all different in T1,T2,T3.make them such that they can point the same boolean object and i would better say use primitive type boolean,you can initialize it in your constructor(like you have done for list) just add one more parameter to your constructor.
.before calling wait(),notify() by some thread,it needs a key associated with the object on which thread is waiting.your code has lack of this requirement.try to understand on what object you need to wait in accordance with your problem.think about it-what is the thing that makes you to wait this thread?what is that condition?and to what object is that condition is directly related with?.when you got a answer,simply wait on that object,in your case it is list(queue).
.read about wait(),notify(),notifyAll().in the way you have implemented these methods is not compatible with your problem.
--->note:notify() only notifies a single thread,and is arbitrary chosen, to end the wait() on the same object,
yes on the same object.read about Condition object and its await(),signal(),signalAll().these methods are analogous to the former three methods with better flexibility.but for understanding it is better to go with former 3 methods as a beginner.
T1 | Acts as a producer to put element in Queue.after it puts,it will notify T2 about it and wait till T3 consumes the element(T1 will get notification for the same from T3.) |
T2 | Acts as a mediator,informs T3 that Queue has been filled,it will wait till T1 notify it and after getting notified T2 will propogate this notification to T3. |
T3 | Acts as a consumer,after it get notified by T2,it needs to consume element from queue and inform about this to T1,it will wait till it is notified by T2. |
this is an example of producer consumer
pattern.
.don't swallow the exception rather set your interruption policy in there,this policy guides the thread during it is interrupted.interruption policy can be termination of thread and print the error or simply print the stack trace informing about where it is interrupted or which line produces a error and this will help you to debug your program,despite of these it can also be some other "xyz" policy depending on your requirements.
In your current code you would have get illegalMonitorException by notify(),wait()-this is because api tells that you cannot wait on an object unless the current thread gets a monitor(key) for it.
though i think it is not easy to solve this problem with wait(),notify(),notifyAll().because when you need to notify T2 through T1 then you may think about notify() but notify() will arbitrary choose b/w T2 and T3 as they both are waiting on the same object which breaks your requirement.and notifyAll() will notify the both threads T2 and T3,again breaking the requirement...so i would better say use the condition object.
I hope these things will help you in rebuilding your code.show us after you are done.and feel free to ask questions or for help if you found some difficulty.
Kind Regards,Praveen.