Thread
类的stop()
方法终止线程执行。当线程停止,它就不能通过start()
方法重新启动。
语法
public final void stop()
public final void stop(Throwable obj)
参数
obj
:要抛出的Throwable
对象。
异常
SecurityException
:如果当前线程无法修改线程,则抛出此异常。
示例
public class JavaStopExp extends Thread
{
public void run()
{
for(int i=1; i<5; i++)
{
try
{
// thread to sleep for 500 milliseconds
sleep(500);
System.out.println(Thread.currentThread().getName());
}catch(InterruptedException e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[])
{
// creating three threads
JavaStopExp t1=new JavaStopExp ();
JavaStopExp t2=new JavaStopExp ();
JavaStopExp t3=new JavaStopExp ();
// call run() method
t1.start();
t2.start();
// stop t3 thread
t3.stop();
System.out.println("Thread t3 is stopped");
}
}
执行上面示例代码,得到以下结果:
Thread t3 is stopped
Thread-0
1
Thread-1
1
Thread-0
2
Thread-1
2
Thread-1
3
Thread-0
3
Thread-1
Thread-0
4
4