Java教程

Java resume()方法

线程类的 resume()方法仅与suspend()方法一起使用。此方法用于恢复使用suspend()方法挂起的线程。此方法允许暂停的线程重新启动。

语法

public final void resume()

返回值

此方法不返回任何值。

Exception

SecurityException : 如果当前线程无法修改该线程。

示例

public class JavaResumeExp 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
        JavaResumeExp t1=new JavaResumeExp ();
        JavaResumeExp t2=new JavaResumeExp ();
        JavaResumeExp t3=new JavaResumeExp ();
        // call run() method
        t1.start();
        t2.start();
        t2.suspend();
        // suspend t2 thread
        // call run() method
        t3.start();
        t2.resume();
    // resume t2 thread
    }
}
输出:
Thread-0
1
Thread-2
1
Thread-1
1
Thread-0
2
Thread-2
2
Thread-1
2
Thread-0
3
Thread-2
3
Thread-1
3
Thread-0
4
Thread-2
4
Thread-1
4

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4