博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程学习笔记(五)
阅读量:6718 次
发布时间:2019-06-25

本文共 1684 字,大约阅读时间需要 5 分钟。

hot3.png

一 . suspend()与resume()方法的使用

        创建MyThread类

public class MyThread extends Thread {            private long i = 0;            public long getI() {                return i;            }            public void setI(long i) {                this.i = i;            }            @Override            public void run() {                while (true) {                    i++;                }            }        }

        创建调用类:

public class Test {            public static void main(String[] args) {                try {                    MyThread myThread = new MyThread();                    myThread.start();                    Thread.sleep(2000);                    myThread.suspend();                    System.out.println(" the value of i = " + myThread.getI() + "and the time is : " + System.currentTimeMillis());                    Thread.sleep(5000);                    System.out.println(" the value of i = " + myThread.getI() + "and the time is : " + System.currentTimeMillis());                    myThread.resume();                    Thread.sleep(5000);                    myThread.suspend();                    System.out.println(" the value of i = " + myThread.getI() + "and the time is : " + System.currentTimeMillis());                    Thread.sleep(5000);                    System.out.println(" the value of i = " + myThread.getI() + "and the time is : " + System.currentTimeMillis());                } catch (Exception e) {                    e.printStackTrace();                }            }}

        运行结果:

        

 

二 .   suspend()与resume()的缺点

        1 . 独占

             在使用suspend()与resume()方法时,如果使用不当,极易造成公共的同步对象的独占,使得其他线程无法访问公共同步对象

        2 . suspend()与resume()的缺点 --- 不同步

        

 

 

 

 

 

转载于:https://my.oschina.net/scymore/blog/739784

你可能感兴趣的文章
多个viewpager可能产生的问题
查看>>
理解自组织:敏捷里的自组织团队都是骗人的
查看>>
weak reference的介绍
查看>>
今年10月初在美国旧金山召开中国(深圳)IT领袖峰会海外论坛
查看>>
Mysql数据库自带四个数据库的解析
查看>>
深层设计
查看>>
OpenCart 之 CSV 格式商品导入
查看>>
Django -- 模型(数据库层)
查看>>
如何安装和配置MariaDB Galera CentOS-7集群
查看>>
腾讯团队
查看>>
编译安装LAMP
查看>>
Java监控神器之psi-probe监控Tomcat和应用
查看>>
vs2010宏
查看>>
2008年最佳Web设计/前端开发技巧、脚本及资源总结
查看>>
[20181116]18c DML 日志优化.txt
查看>>
EDMX 残余表信息清理方法
查看>>
SSH框架的多表查询和增删查改 (方法一)上
查看>>
java日志处理汇总
查看>>
js数组去重的4个方法
查看>>
大量多级分类数据的获取、缓存、搜索查询 怎么设计最快 ?
查看>>