Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying out thread sample

 
raja singh kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My requirement is:

There are three threads T1, T2, T3. We need to have a queue. Thread T1 puts one value into the queue. There will always be only one value in the queue. Whenever thread T1 changes the value in the queue, thread T2 is supposed to inform thread T3 and thread T3 must output the value in the queue

I have used linked list for implementing queue since LinkedList implements the queue interface. I request everyone not to post the solution code but instead guide me on what I am doing wrong or what I should be doing.

Thanks





 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
T1Acts 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.)
T2Acts 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.
T3Acts 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.
 
raja singh kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I can have a counter variable which can act as a flag and can be initially set to 1. T1 after completing its job increments the value to 2. it can notify all threads waiting on it. In T2 thread we can have a check if (counter%3==2) and then T2 can increment the value to 3 and notify all threads waiting. We can have check like for T3 to execute it should check counter%3==0 and then print. There may be some lag in the executing method since threads which are not intended to invoke may get invoked. I dont see any other solution than this.

I was also asking if it is possible to share a value across classes also in another post. But I am still not clear how to do it.
 
Henry Wong
author
Marshal
Pie
Posts: 22112
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:
I was also asking if it is possible to share a value across classes also in another post. But I am still not clear how to do it.


First, from your other topic, you were not referring to "share a value", but instead were referring to sharing variables. Second, additionally, you were also not referring to sharing objects either. Your requirement in the other topic discussed the concept of "sharing reference variable" meaning changes to one reference variable would magically change another. This, of course, is not possible.

You can't share reference variables (the way you described). You can only share objects. So, if you want to share the boolean reference variables, you will need to share the object that contains them. This means, that your runnable instances would need to have references to other runnable instances (the one instance that owns the boolean).

Henry
 
raja singh kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below code is not the solution to the original problem which I had asked. Would the below code be the good way to share values between classes or is there some other better way to do it?

Class A retrieves the boolean value false from the TrySample class and sets it to true. Class B receives the value as true and sets it to false.

 
raja singh kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have. Please let me know where I am going wrong.










 
Henry Wong
author
Marshal
Pie
Posts: 22112
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*** acting as the moderator of this forum, for a bit ***

raja singh kumar wrote:This is what I have. Please let me know where I am going wrong.


Perhaps, it would be a good idea to (1) explain what you have (code), (2) explain what you are seeing, (3) explain what errors you are seeing, and (4) explain what you are expecting too.

Otherwise, it is just a wad of code, being thrown over the wall, with the two words instructions, "what's wrong?". If you were asked to volunteer to answer, would you? Or would you go help those that showed a bit more effort?

Henry
 
Anton Golovin
Ranch Hand
Posts: 527
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:My requirement is:

There are three threads T1, T2, T3. We need to have a queue. Thread T1 puts one value into the queue. There will always be only one value in the queue. Whenever thread T1 changes the value in the queue, thread T2 is supposed to inform thread T3 and thread T3 must output the value in the queue



Hi, Raja,

There is the BlockingQueue and mechanisms for ensuring order in threads running. There is also a queue in java that allows only one element in the queue at any given time - don't remember which one, please look it up.

Take a look at java.utils.concurrent package, it's got all the pieces you are ever likely to need for your task, and they are guaranteed to work correctly unlike custom-written synchronization code which must rely on well-known techniques to be correct.

If you know for sure the order in which your threads need to run, why not write a class that will "compose" this order? Then, your thread classes just have to set their own state and signal that class to go to the next thread to run in its internal queue of threads... In other words, some threads would block until needed?

Or use the ExecutorService - all this functionality is already programmed correctly in there.

With best regards,

Anton.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic