Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to stop Threads


  • From: vincent@xxxxxxxxxxxxxxxx (Vincent Cheesbrough)
  • Subject: How to stop Threads
  • Date: Tue, 29 Feb 2000 15:09:08 +0000

When accessing a boolean (or any other instance variable) from different threads
in this way you must use the volatile modifier:

volatile boolean bRun;

This ensures that a thread uses the actual variable rather than a cached one it
may hold in a register (or whatever)

IFAIK pre java 2 volatile did not have any effect, I beleive that its use is
essential when running on Java 2 VMs (especially ones with hotspot or similar
technologies).

You may also want to use InterruptedException:

public void run()
{
	while(this.bRun)
	{
		try
		{
			....
			// code which either waits/sleeps etc sometimes
			// or uses Thread.interrupted() as shown below
			....
		}
		catch(InterruptedException iex)
		{
			// do nothing=: we may have been interrupted
			// for some reason other than that we were
			// requested to stop using bRun
		}
	}
}

This way you can interrupt wait/Sleep etc calls to provide immediate stopping of
a waiting thread. by doing:
	this.bRun=false;
	thread.interrupt(); // you will need to have kept a reference to the
			    // thread

You can also use this to stop a thread invloved in long winded computation by
periodically doing:
	if(Thread.interrupted())
	{
		throw new InterruptedException();
	}

Periodically in your computation loop.

If you need to do much thread work I recomend the Paul Hyde book "Java Thread
Programming" (ISBN: 0-672-31585-8) as it introduces exactly the sort of issue
you raised and more (and it is up to date).

> Maxime Poulin wrote:
> 
> A clean way would be to have a bRun boolean set somewhere and you could use it
> like this :
> 
> boolean bRun;
> 
> public void run()
> {
>         while(this.bRun)
>         {
>                 ...
>         }
> }
> 
> public void actionPerformed(ActionEvent e)
> {
>         if (e.getActionCommand().equals(START_BUTTON_ACTION_COMMAND)
>         {
>                 this.bRun = true;
>                 (new Thread(this)).start();
>         }
> 
>         else if (e.getActionCommand().equals(STOP_BUTTON_ACTION_COMMAND)
>                 this.bRun = false;
> }
> 
> This way, you thread will die naturally.
> 
> > -----Original Message-----
> > From: hari [mailto:harishreddy@xxxxxxx]
> > Sent: Tuesday, February 29, 2000 12:00 PM
> > To: advanced-java@xxxxxxxxxxxxxxxx
> > Subject: How to stop Threads
> >
> >
> > Hi All,
> >
> > How do we stop thread once it is started ?
> >
> > The thread is started in the actionevent method, and it
> > displays the status of
> > the thhread processing.
> > On the cancel button press of the dialog I want the the thread to be
> > stop the processing and exit from the run method.
> > Any pointers on this .
> >
> >
> > Thanks in ADVANCE.
> > HARI
> >
> > ____________________________________________________________________
> > Get free email and a permanent address at
> http://www.netaddress.com/?N=1
> 
> ---
> To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
> To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx


-- 
-----------------------------------------------------------------------
                                            _                      _ --
                                     __   _(_)_ __   ___ ___ _ __ | |_ 
                                 ____\ \ / / | '_ \ / __/ _ \ '_ \| __|
                                |_____\ V /| | | | | (_|  __/ | | | |_ 
                                       \_/ |_|_| |_|\___\___|_| |_|\__|
                                               vincent@xxxxxxxxxxxxxxxx

---
To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx