adrian
adrian

Reputation: 4594

send data in cycles-threads

If I have a database in which I write data step by step(during an interval of 5 minutes) and as soon I write a new data to the DB I have a client thread which takes it from there and sends it to a remote server.

The problem is that how could I do this:write data,let the client thread that I wrote new data...and to this until I finish writing data in DB?

The writing data in Db is done within onCreate() only the send part is done in a new thread. Thx

Upvotes: 0

Views: 99

Answers (1)

jkhouw1
jkhouw1

Reputation: 7350

how about usuing a queue: BlockingQueue mQueue=new LinkedBlockingQueue();

private class RemoteWriter implements Runnable{
      private final BlockingQueue queue;
      RemoteWriter(BlockingQueue q) { queue=q;}
    public void run() {
      try {
      while (true) {processObject(queue.take());}
      } catch (InterruptedException ex) { //do something
      }
    }
      void processObject(Object o) { 
         //write it to your server
      }
    }


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RemoteWriter rw=new RemoteWriter(mQueue);
    new Thread(rw).start();
            // rest of your code
     }

Then in your worker thread (or ui thread) just pop stuff on the queue.

Upvotes: 1

Related Questions