• 1.8.1suspend与resume方法使用


    暂停线程意味着线程还能恢复运行

    suspend()方法暂停线程。resume()恢复线程

    测试如下

     1 package com.cky.thread;
     2 
     3 /**
     4  * Created by edison on 2017/12/3.
     5  */
     6 public class MyThread extends Thread{
     7     private long i=0;
     8     public long getI() {
     9         return i;
    10     }
    11 
    12     public void setI(long i) {
    13         this.i = i;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         super.run();
    19         while(true) {
    20             i++;
    21         }
    22     }
    23 }
     1 package com.cky.test;
     2 
     3 import com.cky.thread.MyThread;
     4 
     5 /**
     6  * Created by edison on 2017/12/3.
     7  */
     8 public class Test {
     9     public static void main(String[] args) {
    10         try {
    11             MyThread myThread = new MyThread();
    12             myThread.start();
    13             Thread.sleep(5000);
    14 
    15             //A断
    16             myThread.suspend();
    17             System.out.println("A= "+ System.currentTimeMillis()+ " i="+myThread.getI());
    18             Thread.sleep(5000);
    19             System.out.println("A= "+ System.currentTimeMillis()+ " i="+myThread.getI());
    20 
    21             //B段
    22             myThread.resume();
    23             Thread.sleep(5000);
    24 
    25             //c断
    26             myThread.suspend();
    27             System.out.println("B= "+ System.currentTimeMillis()+ " i="+myThread.getI());
    28             Thread.sleep(5000);
    29             System.out.println("B= "+ System.currentTimeMillis()+ " i="+myThread.getI());
    30         } catch (InterruptedException e) {
    31             e.printStackTrace();
    32         }
    33 
    34     }
    35 }
    C:itsoftjdkinjava -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
    ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
    esources.jar;C:itsoftjdkjrelib
    t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
    A= 1512288685100 i=3391077327
    A= 1512288690111 i=3391077327
    B= 1512288695126 i=6819461694
    B= 1512288700129 i=6819461694

    结果分析

    线程确实被暂停了,而且还可以恢复成运行的状态。

  • 相关阅读:
    win_tc使用感受
    10进制转8进制(栈操作)
    动态栈
    数组
    单链表学习
    static用法
    基础2
    linux c first
    linux net command /uboot command
    opencv
  • 原文地址:https://www.cnblogs.com/edison20161121/p/7954772.html
Copyright © 2020-2023  润新知