Discussions

General J2EE: How to wake up thread which is in sleep mode

  1. Hi, I've thread program which continuesly run in background as shown below: [code] public class Counter implements Runnable { int Count; public Counter() { Count=0; } public Counter(int x) { Count=x; } public void run() { while(true) { Count++; System.out.println("Counter="+getCount()); for(int i=1;i<100000;i++) { for(int j=1;j<10000;j++) { // } } try { System.out.println("Sleeping"); Thread.sleep(60000); } catch(InterruptedException e){} } } public int getCount() { return Count; } public static void main(String[] args) { Counter c = new Counter(); Thread t = new Thread(c); t.start(); } } [/code] The above program sleeps for 60sec after certan intervals. However, during some times, when it is asleep, I need to wake it up. I've a front end pages developed in JSP. And if I click Submit, I need to wake this thread if its asleep. Can anyone tell me how to do this? I'll appreciate if u can provide some sample test program. Thanks!
  2. Have a reference to your thread object. You can cache it some where when you start the thread. Thread t = new Thread(c); Cache the reference to "t". On submit of your JSP, get this reference and call t.interrupt();