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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12049369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:24:22  来源:igfitidea点击:

How to pass parameter to an already running thread in java?

javamultithreading

提问by maan81

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 2009 年 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.

我想要一个线程,在其中进行处理并准备好进行键盘输入。另一个线程只是监视网络并将接收到的文本传递给处理线程。

回答by George

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

回答by aioobe

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 ArrayBlockingQueuefor instance, and read up on the Consumer-Producer pattern.

看看ArrayBlockingQueue例如,并阅读消费者 - 生产者模式。

回答by Alstresh

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

回答by dilix

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