This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Elegant way of ending a thread  RSS feed

 
Alex Ioannou
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Here is some code. I want to be able to end this code in an elegant fashion (instead of just hitting ctrl c or the x in the console window).
Something like pressing q to quit would be nice. Is it possible to set up an interrupt that goes off when I hit Q? I don't want to have to set up infinite loops checking for keypresses etc.
Cheers
Al
import java.util.*;
import java.text.*;
public class StartManager extends Thread
{ public void run()
{ // Run the start and reminder tasks programs after 30 mins
Calendar rightnow = Calendar.getInstance();
int hrs = rightnow.get(Calendar.HOUR);
System.out.println("WF Event Manager - Running...");

while(true)
{ String checktime = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new java.util.Date());

rightnow = Calendar.getInstance();
int check = rightnow.get(Calendar.HOUR);

if(hrs != check)
{ // An hour has elapsed so start the task checks.
System.out.println("WF Event Manager - 1 Hour has elapsed - starting checks...");
hrs = check;
ReminderTask rtask = new ReminderTask();
StartTask stask = new StartTask();
rtask.start();
stask.start();
}

else
{ System.out.println("WF Event Manager - testing time - " + checktime);
try
{ // 30 mins is 1800000 nanoseconds
this.sleep(1800000);
}

catch(InterruptedException e){}
}
}
}
}
 
Mo Ibrahim
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a thread with keylistener which modifies a boolean flag value to false when a key is pressed, say Q, and let your StartManager loop on this flag.
 
Alex Ioannou
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I have now turned my code into a component and added all the listener stuff. One final problem remains. When the thread aint running it's sleeping. And this thread is only awake for say 10 seconds every hour. Listeners don't work on sleeping threads.
Any suggestions?
import java.util.*;
import java.text.*;
import java.awt.event.*;
import java.awt.*;
public class StartManager extends Component implements KeyListener, Runnable
{ boolean test;
public void run()
{ // Run the start and reminder tasks programs after every hour, starting from the thread creation.
Calendar rightnow = Calendar.getInstance();
int hrs = rightnow.get(Calendar.HOUR);
System.out.println("WF Event Manager - Running...");
test = true;
this.addKeyListener(this);

while(test)
{ String checktime = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new java.util.Date());

rightnow = Calendar.getInstance();
int check = rightnow.get(Calendar.HOUR);

if(hrs != check)
{ // An hour has elapsed so start the task checks.
System.out.println("WF Event Manager - 1 Hour has elapsed - starting checks...");
hrs = check;
ReminderTask rtask = new ReminderTask();
StartTask stask = new StartTask();
rtask.start();
stask.start();
}

else
{ System.out.println("WF Event Manager - testing time - " + checktime);
try
{ // 30 mins is 1800000 nanoseconds
Thread.sleep(1800000);
}

catch(InterruptedException e){}
}
}
}

public void keyPressed(KeyEvent e){ test = false;}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!