6

Hi im a beginner so sorry for my question if it sounds naive.

I want to implement a thread that runs in the background and listens all the time. By listening i mean, say it keeps check on a value returned from main thread and if the vaue exceeds certain figure, it executes some method, or say exits the program.

If you could give me some idea or at least refer me to something useful, that'll be great.

Space Rocker
  • 787
  • 3
  • 11
  • 25

5 Answers5

2

You wouldn't want this thread to run in a loop, continuously polling the value, because that would waste processing.

Ideally, the listener would be actively notified when the value had changed. This would require that any code that modifies the monitored value call a special method. It might not be necessary for the listener to run in a separate thread; it would depend on what the listener does when it is notified.

If the code that modifies the value cannot be changed, then the best you can do is to check the value at intervals. You won't see a change immediately, and its possible that you could miss changes altogether because the value is changing multiple times during an interval.

Which of these solutions fits your situation the best?

erickson
  • 265,237
  • 58
  • 395
  • 493
  • Thank you for your answer. I was thinking about implementing a loop initially. Actually I am executing an external process through windows command line, and in return i read a value which i need to keep track off. I get the response in String format and cast it back to int to apply the checks. I was thinking to keep calling this external process through Runtime.getRuntime().exec() in a loop and check the value at regular intervals say every 2 seconds. It is not that sensitive to be in msec. Do you think it is a wise idea doing it like this? – Space Rocker Dec 23 '10 at 01:20
  • @Sara - Your plan sounds like an appropriate way to handle it. I would recommend looking at the [`ScheduledExecutorService`](http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html) to create a thread that will run this check at a fixed interval. – erickson Dec 23 '10 at 05:30
  • [This StackOverflow question](http://stackoverflow.com/questions/426758/running-a-java-thread-in-intervals) and accepted answer provide a good example of how to use the `ScheduledExecutorService`. – erickson Dec 23 '10 at 05:33
1

You can find some information on using threads in Java Tutorials (if you are new to concurrency in Java I recommend reading this tutorial first). Especially this section may be useful for you (it shows how to create and start a new thread).

Piotr
  • 5,543
  • 1
  • 31
  • 37
1

If you simply need to poll for a result from another thread try using java.util.concurrent package as suggested by @Piotr. Here's a concrete example how you might do this:

import java.util.concurrent.*;

class Main{
    public static void main(String[] args) throws Exception{
        //Create a service for executing tasks in a separate thread
        ExecutorService ex = Executors.newSingleThreadExecutor();
        //Submit a task with Integer return value to the service
        Future<Integer> otherThread = ex.submit(new Callable<Integer>(){
            public Integer call(){
                //do you main logic here
                return 999;//return the desired result
            }
        }

        //you can do other stuff here (the main thread)
        //independently of the main logic (in a separate thread)

        //This will poll for the result from the main
        //logic and put it into "result" when it's available
        Integer result = otherTread.get();

        //whatever you wanna do with your result
    }
}

Hope this helps.

rodion
  • 14,729
  • 3
  • 53
  • 55
0

I guess you can simply use a gui component for the main thread like a JTextField, then read about Event Handling and you will be easily able to listen to the state change of the text field input values.

Ahmed
  • 3,398
  • 12
  • 45
  • 64
0

action listner is what you are looking for. check it out here http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/ActionListener.html

Nathan Boyd
  • 579
  • 3
  • 8