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

后端 未结 4 1124
独厮守ぢ
独厮守ぢ 2021-01-02 18:49

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

Something similar to a comme

相关标签:
4条回答
  • 2021-01-02 19:14
    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);
        }   
    }
    
    0 讨论(0)
  • 2021-01-02 19:19

    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);
    
    0 讨论(0)
  • 2021-01-02 19:35

    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.

    0 讨论(0)
  • 2021-01-02 19:40

    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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题