File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Synchornize TimerTask and Thread??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Synchornize TimerTask and Thread???" Watch "Synchornize TimerTask and Thread???" New topic

Synchornize TimerTask and Thread???

Terence Fernandes

Joined: Oct 25, 2005
Posts: 4
Hi Everyone,

Im having a TimerTask thread which runs in an indefite loop unless I stop it. At the same time I have a Thread class implementing runnable interface which I have to run on the same system.
I created a Main class file which contain calls to running both the TimerTask and the Thread class.....but wht is happening is the Timer Task runs for the first moment and then for the remaining of the Time the Thread class runs without giving any control to the TimerTask. Is there any way I can get both these threads to Synchronize???

Adding the code for you review of the TimerTask, this code runs the Thread in an Idefinite loop.

public class Reminder {
Timer timer;
int delay = 5000;
int period = 1000;

public Reminder() {
timer = new Timer();
timer.schedule(new RemindTask(), delay,period);

class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
//timer.cancel(); //Terminate the timer thread

public static void main(String args[]) {
new Reminder();
System.out.println("Task scheduled.");

The code below is for the Thread class I need this THread to check if the survey has been completed and promt if it has not....If the survey has been ocmpleted i need it to exit.

public class PickSurveyThread implements Runnable {

public void run(){


}catch(Exception e){}

System.out.println("Has the survey been submitted "+survey);
System.out.println("Complete the Survey");
System.out.println("Thank you for completing the survey");
}while(survey.equalsIgnoreCase("no"));//This is one condition I checl
dataObj = null;
System.out.println("In survey thread");


And this is the Main file which calls these threads.....

import java.util.Date;
import java.util.Calendar;
public class PickThreadsMain {

public static void main(String args[]){
ConfigDataObj dataObj = new ConfigDataObj();
String surSubmitted = dataObj.getSurSubmitted();

//Start Thread to Check if user has to update his user profile PickThreads pt = new PickThreads();
// Start Thread to check if user has to submit his survey.
PickSurveyThread ps = new PickSurveyThread();
Thread th2 = new Thread(ps);


Any help will be much appreciated.

Thanks guys,

I agree. Here's the link:
subject: Synchornize TimerTask and Thread???
It's not a secret anymore!