栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

线程的挂起、继续执行和停止

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线程的挂起、继续执行和停止

final void resume()          //重启(继续执行)

final void suspend()          //暂停(挂起)

final void stop() //停止

说明:这三个方法在Java2中就被摒弃了,无法继续使用来控制线程。

但是线程的设计必须有一个run()方法来周期性的检查他,以确定线程是否应该挂起、继续执行还是停止。通常,这是依靠两个指标变量来完成的。一个用于挂起和继续执行,另一个用于停止。

public class MyThread5 implements Runnable{
    Thread thread;
    boolean suspended;
    boolean stopped;
    MyThread5(String name) {
        thread = new Thread(this, name);
        suspended = false;
        stopped = false;
    }
    public static MyThread5 cteateAndStart(String name) {
        MyThread5 myThread = new MyThread5(name);
        myThread.thread.start();
        return myThread;
    }
    public void run() {
        System.out.println(thread.getName() + " starting.");
        try {
            for (int i = 1; i < 1000; i++) {
                System.out.print(i + " ");
                if ((i%10)==0) {
                    System.out.println();
                    Thread.sleep(250);
                }
                synchronized(this) {
                    while (suspended) {
                        wait();
                    }
                    if (stopped) break;
                }
            }
        }catch (InterruptedException exception) {
            System.out.println(thread.getName() + " interrupted.");
        }
        System.out.println(thread.getName() + " exiting.");
    }
    //停止执行线程
    synchronized void mystop() {
        stopped = true;
        suspended = false;
        notify();
    }
    //暂停
    synchronized void mysusposend() {
        suspended = true;
    }
    //继续执行
    synchronized void myresume() {
        suspended = false;
        notify();
    }
}


public class Suspended {
    public static void main(String[] args) {
        MyThread5 mt1 = MyThread5.cteateAndStart("My Thread");
        try {
            Thread.sleep(1000);

            mt1.mysusposend();
            System.out.println("Suspendeding thread.");
            Thread.sleep(1000);

            mt1.myresume();
            System.out.println("Resuming thread.");
            Thread.sleep(1000);

            mt1.mysusposend();
            System.out.println("Suspendeding thread.");
            Thread.sleep(1000);

            mt1.myresume();
            System.out.println("Resuming thread.");
            Thread.sleep(1000);

            //mt1.mystop();
            mt1.mysusposend();
            System.out.println("Stopping thread.");
            mt1.mystop();
        }catch (InterruptedException exception) {
            System.out.println("Main thread Interrupted.");
        }
        try {
            mt1.thread.join();
        }catch (InterruptedException exception) {
            System.out.println("Main thread Interrupted.");
        }
        System.out.println("Main thread exiting.");
    }
}

通过控制参数的boolean值来实现

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820418.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号