• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending a value from one thread to other threads

 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hii there,

Iam using multiple threads and I have initialized a variable in one thread and now i need to send that value to all the other threads.Neither the sending thread nor any of the other threads are near completion.When i send this value i want the other threads to first store this variable's value and then continue with their work.
How can i achieve this?Suggest a code snippet.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's distinguish between the threads and the objects that are executing some code on them. Say our main thread t1 starts a second thread t2.

We have a reference to that runnable and we can call a setter now or later. This code executes on thread t1 and updates the runnable:

The runnable can see the change next time it looks, in this case the next time through a loop:

Does that much make sense? If we're ok that far, we can dig into a few wrinkles that make it just a bit trickier.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stan James
But i may have a problem here.I don't have any references to the different threads in the main thread,here is what i did...



I think what you are suggesting is something similar to Polling.But none of the threads should wait for the value to be sent.Is there a way to interrupt the thread and ask it to do what we want and then continue with it's work.Or do i again need to create a thread within the thread which keeps on checking if the value has changed.I was hoping there would be a better solution.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just adding one more bit.I want the value to be sent between all the child threads that the main thread has created and not the child thread and main thread.i.e. One child thread has a value which should be sent to all the other child threads.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i created a message passing library long ago to do that. I think Java may have its own message passing library for that now. Barring that, you will have to create your own architecture to do this. all you need to do is notify each child, and keep a record to ensure that each child has obtained the value it was notified that it should obtain.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Lamont Gilbert...My requirement may not be satisfied with a wait and notify call since the threads are running normally and perform some other work if and only if a value is passed to it(It need not wait to receive that value).Can you all suggest some other way?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something will have to have references to all the runnables and do something similar to what I showed before. It doesn't really matter what object or thread calls the set methods. See the "publish-subscribe with intermediary" section of this messaging patterns page for another option.

If your runnable is in a loop, it can sense the variable change the next time through the loop. If it's doing a long sequence of steps and not a loop, things get trickier. You could use interrupt() and interrupted(). Be aware that your runnable may be doing something uninterruptable like blocking IO, so it may not sense the interruption immediately. And you'll need references to Threads instead of Runnables.
 
Davis Swan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do this sort of thing by using a static class that contains static members which all of the threads can check and/or update. It acts like a global variable in 3GLs. A code snippet follows;

import java.*;

public class Globals
{
static boolean DEBUG = false;
}

Any thread can then use the member Globals.DEBUG.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Davis Swan
I had done the same thing....used a class with only static variables and synchronized methods to act a central data resource for all the threads.But initially i couldn't use that because i had a very long loop running,so i cut down the loop to a sufficiently small one which now works fine.
Thanks for the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!