7

How to pass parameter to an already running thread in java -- not in the constructor, & probably without using wait() (possible ??)

Something similar to a comment in How can I pass a parameter to a Java Thread?

Do you mean passing a parameter to an already running thread ? Because all the current answers are about passing parameters to new threads... – Valentin Rocher May 18 '09 at 10:43

[edited]

yes, I was looking for something like the producer/consumer pattern.

I wanted something like a thread in which has the processing & is ready for keyboard input. The other thread is just to monitor network and pass on the received text to the processing thread.

Community
  • 1
  • 1
maan81
  • 3,469
  • 8
  • 36
  • 53

4 Answers4

12

Maybe what you really need is blocking queue.When you create the thread, you pass the blocking queue in and the thread should keep checking if there is any element in the queue. Outside the thread, you can put elements to the queue while the thread is "running". Blocking queue can prevent the thread from quit if their is nothing to do.

public class Test {
    public static void main(String... args) {

        final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
        Thread running = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        String data = queue.take();
                        //handle the data
                    } catch (InterruptedException e) {
                        System.err.println("Error occurred:" + e);
                    }
                }
            }
        });

        running.start();
        // Send data to the running thread
        for (int i = 0; i < 10; i++) {
            queue.offer("data " + i);
        }
    }
}
lxnx
  • 194
  • 2
  • 17
George
  • 4,029
  • 2
  • 22
  • 26
4

The "other thread" will have its own life, so you can't really communicate with it / pass parameters to it, unless it actively reads what you gives to it.

A thread which you allows you to communicate with it typically reads data from some buffered queue.

Have a look at ArrayBlockingQueue for instance, and read up on the Consumer-Producer pattern.

aioobe
  • 413,195
  • 112
  • 811
  • 826
2
public class T1 implements Runnable {
    //parameter of thread T1
    public static AtomicBoolean flag = new AtomicBoolean();

    @Override
    public void run() { 
    }   
}

public class T2 implements Runnable {

    @Override
    public void run() { 
        //parameter to an already running thread
        T1.flag.set(true);
    }   
}
Alstresh
  • 583
  • 2
  • 4
  • 16
0

What about such way:

    class TestRun implements Runnable
    {
        private int testInt = -1;

        public void setInt(int i)
        {
            this.testInt = i;
        }

        @Override
        public void run()
        {
            while (!isFinishing())
            {
                System.out.println("Working thread, int : " + testInt);
                try
                {
                    Thread.sleep(2500);
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

.....

        TestRun first = new TestRun();
        TestRun second = new TestRun();
        (new Thread(first)).start();
        (new Thread(second)).start();
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException e)
        {
        }
        first.setInt(101);
        second.setInt(102);
dilix
  • 3,761
  • 3
  • 31
  • 55