简体   繁体   English

如何将参数传递给java中已经运行的线程?

[英]How to pass parameter to an already running thread in java?

How to pass parameter to an already running thread in java -- not in the constructor, & probably without using wait() (possible ??) 如何将参数传递给java中已经运行的线程 - 而不是在构造函数中,并且可能没有使用wait()(可能??)

Something similar to a comment in How can I pass a parameter to a Java Thread? 类似于“ 如何将参数传递给Java线程”中的注释

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 因为所有当前的答案都是关于将参数传递给新线程... - Valentin Rocher 09年5月18日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. 另一个线程只是监视网络并将接收到的文本传递给处理线程。

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);
        }
    }
}

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. 例如,看看ArrayBlockingQueue ,并阅读Consumer-Producer模式。

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);
    }   
}

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM