Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchornize TimerTask and Thread???

 
Terence Fernandes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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(){

try{

Thread.sleep(5000);
}catch(Exception e){}
do{

System.out.println("Has the survey been submitted "+survey);
if(survey.equalsIgnoreCase("no")){
System.out.println("Complete the Survey");
}
else{
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);
th2.start();


}

Any help will be much appreciated.

Thanks guys,

Terence
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic