TimerTasks in Swing
I'm trying to write a message acknowledgement application where I need to: a) Add every new message on a message queue. Just using an Arraylist for creating a message queue. b) Notify a timertask to expect an acknowledge in 50 sec for the message so make it to either sleep for 50sec or wake up when an acknowledgement is re开发者_JAVA百科ceived.
What is the best practice to implement this?
Thanks
I'm not quite clear what your needs are. What does this have to do with Swing or timers? What kind of threading are you dealing with here? I'll make some assumptions and suggest a couple things.
It sounds like you want to put a message in a queue, then wait until a response is received, or 50s max. You should check out BlockingQueue. It is thread-safe, and you can wait for a specific amount of time for another thread to put something in it. This seems like it could be useful for message/acknowledge problem.
BlockingQueue<MSG> queue = new LinkedBlockingQueue<MSG>();
// put a message in the queue
queue.put( msg );
// have a thread wait on the queue until something is available in it
MSG msg = queue.poll( 50, TimeUnit.SECONDS );
I need more details on your problem for more specific help.
i thought u wanted to have some mechanism, where updation needs after particular intervals, you can use the thread and set a sleep as per interval requirement like :
public void run() {
while (Start == true) {
getMessage(); //yourmethod();
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
stop();
}
}
}
精彩评论