Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Timer thread problem

Posted on 2006-04-14
5
Medium Priority
?
191 Views
Last Modified: 2011-08-18
Hi Friends,
i have wrote a timer class,that should repeatedly check to see if new message has arrived and if a new message is there,it should call the SendMessage code to send a message back to the originator.Can someone please correct my code:

public class Test{
      Timer timer = null;
        
    public Test(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(),0,seconds*1000);
    }
 
    class RemindTask extends TimerTask {
        public void run() {
              ReadMessages rm = new ReadMessages();
              Message rmsg = rm.getMessage();
              System.out.println("Message is:" + rmsg);
               
           
                   CSDAO csDAO = new CSDAO();
                  String response1 = null;
                  try{
                        response1 = csDAO.getCoupon(rmsg.getText());
                  }catch(Exception e){
                        e.printStackTrace();
                  }
 
        /* I want this to be called only once */
                System.out.println("Response1 is:" + response1);
                SendMessage sm = new SendMessage();
                sm.setMessage(rmsg.getOriginator(),response1);
                
      //timer.cancel(); //Terminate the timer thread
        }
    }
 
 
 
public static void main(String[] ar) {
       //  while(true){
         System.out.println("About to schedule task.");
       new Test(10);
       System.out.println("Task scheduled.");
         //}
 
}
 
}



So,basically I want that when a new message is received ,a new process() should be genarated and it should handle the SendMessage task while the original process keeps polling for received messages.
Please help.
Thanks
0
Comment
Question by:harmeek_80
  • 2
3 Comments
 
LVL 2

Expert Comment

by:gauravkrtomar
ID: 16464042
I am not able to find any flaw in your program. Please elaborate more on what you think is wrong with your code, coz I ran this code and it did check for the messages after every 10 secs and then sends the messages.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16464170
>> /* I want this to be called only once */

Not possible because you have it in the run () method which will be called every time the timer 'ticks'.
0
 
LVL 2

Accepted Solution

by:
gauravkrtomar earned 300 total points
ID: 16464188
u can have a static variable in RemindTask class, let say:-
static boolean flag;

and just set it to true if u r running that code
 >> /* I want this to be called only once */

and just before executing that code check whether that code has been executed or not:-
if(!flag){
              flag=true;
              System.out.println("Response1 is:" + response1);
              SendMessage sm = new SendMessage();
              sm.setMessage(rmsg.getOriginator(),response1);
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question