• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Elegant way of ending a thread

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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){}
}
}
}
}
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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){}
}
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic