Timer thread problem

Posted on 2006-04-14
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;
                        response1 = csDAO.getCoupon(rmsg.getText());
                  }catch(Exception e){
        /* I want this to be called only once */
                System.out.println("Response1 is:" + response1);
                SendMessage sm = new SendMessage();
      //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.
Question by:harmeek_80
    LVL 2

    Expert Comment

    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.
    LVL 30

    Expert Comment

    >> /* 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'.
    LVL 2

    Accepted Solution

    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:-
                  System.out.println("Response1 is:" + response1);
                  SendMessage sm = new SendMessage();

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now