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: mpoulin@xxxxxxxxxxxxxxxxx (Maxime Poulin)
  • Subject: How to stop Threads
  • Date: Tue, 29 Feb 2000 09:27:46 -0500

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01BF82C2.A60B70C0
Content-Type: text/plain;
	charset="iso-8859-1"

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

------_=_NextPart_001_01BF82C2.A60B70C0
Content-Type: text/html;
	charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2650.12">
<TITLE>RE: How to stop Threads</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>A clean way would be to have a bRun boolean set somewhere and you could use it like this :</FONT>
</P>

<P><FONT SIZE=2>boolean bRun;</FONT>
</P>

<P><FONT SIZE=2>public void run()</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>while(this.bRun)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>...</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>}</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>

<P><FONT SIZE=2>public void actionPerformed(ActionEvent e)</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>if (e.getActionCommand().equals(START_BUTTON_ACTION_COMMAND)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>this.bRun = true;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>(new Thread(this)).start();</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>}</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>else if (e.getActionCommand().equals(STOP_BUTTON_ACTION_COMMAND)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>this.bRun = false;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>

<P><FONT SIZE=2>This way, you thread will die naturally.</FONT>
</P>

<P><FONT SIZE=2>&gt; -----Original Message-----</FONT>
<BR><FONT SIZE=2>&gt; From: hari [<A HREF="mailto:harishreddy@xxxxxxx";>mailto:harishreddy@xxxxxxx</A>]</FONT>
<BR><FONT SIZE=2>&gt; Sent: Tuesday, February 29, 2000 12:00 PM</FONT>
<BR><FONT SIZE=2>&gt; To: advanced-java@xxxxxxxxxxxxxxxx</FONT>
<BR><FONT SIZE=2>&gt; Subject: How to stop Threads</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; Hi All,</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; How do we stop thread once it is started ?</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; The thread is started in the actionevent method, and it </FONT>
<BR><FONT SIZE=2>&gt; displays the status of</FONT>
<BR><FONT SIZE=2>&gt; the thhread processing.</FONT>
<BR><FONT SIZE=2>&gt; On the cancel button press of the dialog I want the the thread to be </FONT>
<BR><FONT SIZE=2>&gt; stop the processing and exit from the run method.</FONT>
<BR><FONT SIZE=2>&gt; Any pointers on this .</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; Thanks in ADVANCE.</FONT>
<BR><FONT SIZE=2>&gt; HARI</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; ____________________________________________________________________</FONT>
<BR><FONT SIZE=2>&gt; Get free email and a permanent address at </FONT>
<BR><FONT SIZE=2><A HREF="http://www.netaddress.com/?N=1"; TARGET="_blank">http://www.netaddress.com/?N=1</A></FONT>
</P>

<P><FONT SIZE=2>---</FONT>
<BR><FONT SIZE=2>To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx</FONT>
<BR><FONT SIZE=2>To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx</FONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01BF82C2.A60B70C0--

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