Domanda

The idea is:

I have a main class, inside this class I want to create a thread. That thread must return data each 10 minutes (I know that it is not possible with a normal thread implementation).

I saw some implementation about Callable or ScheduledExecuter, but I can't adapt it.

I need it, because during the program execution my database are updating. So I want a thread that each 10 minute executes a query and return me the result.

Thanks.

È stato utile?

Soluzione

A simple Callable Solution

interface Callable {
    void call(Data d);
}

class MyThread implements Runnable{
    Callable callable;

    public MyThread(Callable c){
        callable = c;
    }
    void run(){
        while(true){
             callable.call(/** pass your data */);
             //sleep 10 minutes
        }
    }
}

You can now create a MyThread object from your code, and pass it an Callable. You can do this with an anonymous class

MyThread t = new MyThread(new Callable(){
    void call(Data d){
        //process data here
    }
});

Altri suggerimenti

When you want to schedule something to happen at regular intervals, you can use a java.util.Timer with one or more java.util.TimerTasks.

But whether you use timers or implement your own time scheduling with your own threads, you need some way to communicate from your Threads/TimerTasks with the objects in your main thread. You can implement this by passing these objects to these threads and then call methods on them in your run-method. But keep in mind that you have no control over what the main thread is doing when that method is called from the sub-thread. It might happen from time to time that the sub-thread changes a value while the main-thread is currently performing some operation with it. This can lead to strange bugs commonly referred to as race conditions. Take this example:

class ValueList {

    private List<Integer> values = new ArrayList<>();

    // this method may be called from many different threads to add values
    public void add(Integer i) {
        values.add(i);
    }

    // this method is called from the main thread to update the GUI
    public int getAverage() {
         int sum = 0;
         for (Integer i: values) {
             sum += i;
         }
         // Imagine a thread calls add(Integer) when the main threads 
         // execution is exactly here!
         // the average will be too low because the new value was 
         // not yet counted for the sum, but is now accounted for 
         // when calculating the average from the sum.             
         return sum / values.size();
    }
}

To prevent this from happening, familiarize yourself with the various synchronization features offered by Java.

Threads all exist within the same memory space of the parent process, so it's actually quite trivial to pass data between threads. The most basic way to do so is to simply overwrite a common memory location, (say a string in a common object both threads know about) though not very good practice.

When doing multi-threaded data, you need to be careful about semantics, as it's a common mistake to introduce race conditions and all sorts of other badness when implementing a multi-threaded application.

For just one example: http://en.wikipedia.org/wiki/Producer–consumer_problem

Looking at the java.lang.concurrency classes may give you some idea of how to pass data safely between threads, but please consider that this is a fairly complex area of computer science, and plan your study time appropriately: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/package-tree.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top