aspose file tools*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Synchornize TimerTask and Thread???" Watch "Synchornize TimerTask and Thread???" New topic
Author

Synchornize TimerTask and Thread???

Terence Fernandes
Greenhorn

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(){

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchornize TimerTask and Thread???