Java Thread resume()方法

java.lang.Thread.resume() 方法仅与suspend() 方法一起使用。此方法用于恢复使用suspend() 方法挂起的线程。此方法允许挂起的线程重新启动。

1 语法

public final void resume()  

2 参数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.lang.Thread.resume()方法的例子
 */
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();
        t2.suspend(); // suspend t2 thread
        // call run() method
        t3.start();
        t2.resume(); // resume t2 thread
    }
}

输出结果为:

Thread-1
Thread-0
1
1
Thread-2
1
Thread-0
2
Thread-1
2
Thread-2
2
Thread-0
3
Thread-1
3
Thread-2
3
Thread-1
4
Thread-0
4
Thread-2
4

热门文章

优秀文章