-1

I need to be able to 'send' a string from one thread to 1 or more other threads. But I have no idea how to do this.

Basically, I have a server who has one connection that sends commands to it. I need to send these commands to all the other threads, so they can send them to their clients.

How can I have a single string that is referenced by all the other threads. How to know when all the threads executed the command string?

JensG
  • 13,148
  • 4
  • 45
  • 55
Phin46
  • 22
  • 1
  • Post some code........... – David Xu Jul 27 '14 at 09:19
  • His name is "Phin46" with an __'n'__. As for the question, it appears to be off-topic as it is too broad. – Unihedron Jul 27 '14 at 09:28
  • I know its really broad but theres way to much code to post like 15 plus classes – Phin46 Jul 27 '14 at 09:29
  • and im just looking to understand string communication and how to know when all the threads have looked at some string – Phin46 Jul 27 '14 at 09:31
  • "...String communication..." There is no such topic. Passing a reference to a string around is no different from passing a reference to any other object. – Solomon Slow Jul 27 '14 at 22:42
  • 1
    "...all the other threads..." I've got a feeling that your problem runs deeper than just how to hand off an object reference from one thread to another. (Hint: use a queue) I've got a feeling that you are still struggling to define the responsibilities of each thread. (Hint: Each thread usually should loop, waiting for _just one_ kind of event, and then it should do _just one_ thing to handle the event each time it happens.) You really should post some code that illustrates the structure of your program instead of just talking about it. – Solomon Slow Jul 27 '14 at 22:49

3 Answers3

0

If I understand you right, this is your setup:

setup

How can I have a single string that is referenced by all the other threads?

When the string is sent via sockets or similar, it will be a different string. But with the same content, and that's what counts here. So I would not care too much about this point.

How to know when all the threads executed the command string?

Have each thread sent back a confirmation to the server whenever the thread finished processing a command for all clients. The server keeps track of all commands sent and confirmations received.

Keep in mind, that threads may crash, connections may break and the execution of a command may not succeed in a timely fashion, or fail entirely.

JensG
  • 13,148
  • 4
  • 45
  • 55
0

Somewhere you will need a List of your Runnabless like:

List<MyRunnable> runningThreads;

Then you will have an implementation of Runnable:

class MyRunnable implements Runnable {
    public void run() { ... }
}

Now you need to have some way of sending a message to that Runnable.

class MyRunnable implements Runnable {
    public void run() { ... }
    public void sendMessage( String message ){ ... }
}

So to send all the runnable a message it's as easy as:

for( MyRunnable runnable : runningThreads ){
    sendMessage( "Hello There!" );
}

What to do now depends heavily on what you want to do next with the message. In any way it has to appear somehow in the Thread's visible range. So for starters lets save it in a variable:

class MyRunnable implements Runnable {
    private volatile String myLastMessage;
    public void run() { ... }
    public void sendMessage( String message ){
        this.myLastMessage = message;
}

so if you're run is already run periodically you can get off with:

public void run(){

    while( true ){

         Thread.sleep( 1000 ); //1s
         if( lastMessage != null ){
             doSomethingWith( lastMessage );
             lastMessage = null;
         }
    }
}

If you need more than one message stored in the Thread you can use e.g. SynchronizedList for this.

If you need your Thread to react instantly on the message it received then use a monitor and notifyAll method. See e.g. here: http://www.programcreek.com/2009/02/notify-and-wait-example/

Scheintod
  • 7,953
  • 9
  • 42
  • 61
0

Perhaps you are refering to the Observer pattern (aka, Publish–subscribe pattern). The server (publisher) needs to know their clients (subscribers) in order to send a common message, so you need a data structure. There are several ways to implement this. See the next links:

Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148