Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > non-stop thread

Reply
Thread Tools

non-stop thread

 
 
Steve Horsley
Guest
Posts: n/a
 
      07-07-2003
On Sun, 06 Jul 2003 19:05:55 -0700, Peter wrote:

> Thanks for the opinions
> If implement Runnable, and you call "sleep()" within run(). After
> you call interrupt(), then thread won't stop too.
>
> from Peter


It's not clear whether you want the thread to stop or not.
Anyway, here's an example where the thread doesn't stop when interrupted.
Uncomment the line in the loop to make it stop when it is interrupted.

Steve


/** QuickTest.java */
public class QuickTest implements Runnable {

private volatile boolean shouldRun = false;

public static void main(String[] args) {
// Create and start a QuickTest
QuickTest qt = new QuickTest();
Thread t = new Thread(qt);
t.start();

// wait 5 seconds
try {
Thread.sleep(5000);
} catch(InterruptedException ie) {
System.out.println("Main thread interrupted - shouldn't happen");
}

// interrupt the test thread
t.interrupt();

// wait another 5 seconds
try {
Thread.sleep(5000);
} catch(InterruptedException ie) {
System.out.println("Main thread interrupted - shouldn't happen");
}
}

/** Loop forever. */
public void run() {
shouldRun = true;
while(shouldRun) {
System.out.println("Snooze...");
try {
Thread.sleep(1000);
} catch(InterruptedException ie) {
System.out.println("Sleeper loop was interrupted.");
shouldRun = false;
}
}
System.out.println("Sleeper loop terminating");
}



}
//================================================== ===========




 
Reply With Quote
 
 
 
 
Steve Horsley
Guest
Posts: n/a
 
      07-07-2003
On Mon, 07 Jul 2003 10:40:51 +0100, Steve Horsley wrote:

> On Sun, 06 Jul 2003 19:05:55 -0700, Peter wrote:
>
>
> It's not clear whether you want the thread to stop or not.
> Anyway, here's an example where the thread doesn't stop when interrupted.
> Uncomment the line in the loop to make it stop when it is interrupted.
>


Oops, I lied.
The posted example _does_ stop. Comment out the line:
shouldRun = false;
to make it carry on even after an interrupt.

Steve
 
Reply With Quote
 
 
 
 
Dale King
Guest
Posts: n/a
 
      07-08-2003
"Steve Horsley" <(E-Mail Removed)_SPAM.net> wrote in message
news(E-Mail Removed)_SPAM .net...
> On Thu, 03 Jul 2003 23:23:54 -0700, Peter wrote:
>
> > Hi
> > I have a class that extend Thread. If i put this line
> > "thread.currentThread().sleep(1000);" into the "public void run()",
> > after i call interrupt(), the thread will stop.
> >
> > thanks
> > from Peter ((E-Mail Removed))

>
> Do you have a question? If so, I suggest you post some code with it so
> others can see more clearly what you're on about.



I'm gathering that his question involves the fact that the thread is not
interrupted at the instant that interrupt is called on it. If that is the
question, then that is the way it is supposed to work.

It will only be interrupted when in a wait, join, sleep, or blocked in
certain I/O operations in the New I/O API. If it is not currently blocked
all that will happen is that the threads interrupted flag is set so that it
will be interrupted when it does do a blocking operation or you can also
query the status of the interrupt flag with the interrupted method (which
also clears the flag).
--
Dale King


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
Thread was being aborted thrown for background thread (win2003 ser =?Utf-8?B?Sm9oYW5uYQ==?= ASP .Net 3 10-15-2004 01:35 PM
Thread was being aborted in win2003 server. Back ground thread reading MS access database, no redirects or transfers. Johanna ASP .Net 0 10-13-2004 01:32 PM
"Thread was being aborted" error from WebApp using Thread.Sleep. Stephen Miller ASP .Net 3 07-01-2004 11:50 PM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM



Advertisments