Java Thread stop()方法

java.lang.Thread.stop() 方法终止线程执行。线程一旦停止,就无法通过start()方法重新启动。

1 语法

public final void stop()  
public final void stop(Throwable obj)  

2 参数

obj:要抛出的Throwable对象。

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.lang.Thread.stop()方法的例子
 */
public class Demo 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
        Demo t1=new Demo ();
        Demo t2=new Demo ();
        Demo t3=new Demo ();
        // call run() method
        t1.start();
        t2.start();
        // stop t3 thread
        t3.stop();
        System.out.println("Thread t3 is stopped");
    }
}

输出结果为:

Thread t3 is stopped
Thread-1
Thread-0
1
1
Thread-0
2
Thread-1
2
Thread-0
3
Thread-1
3
Thread-1
4
Thread-0
4

热门文章

优秀文章