• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Synchornize TimerTask and Thread???

Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,

Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic