1 /**
2 * 线程的暂停、恢复和停止
3 * @author Administrator
4 *
5 */
6 public class ThreadControlDemo {
7 public static void main(String[] args) {
8 MyThread mt = new MyThread("MyThread");
9
10 try {
11 Thread.sleep(3000);
12 System.out.println("\nSuspending MyThread.");
13 mt.mySuspend();
14 Thread.sleep(3000);
15
16 System.out.println("\nResuming MyThread.");
17 mt.myResume();
18 Thread.sleep(3000);
19
20 System.out.println("\nSuspending MyThread again.");
21 mt.mySuspend();
22 Thread.sleep(3000);
23
24 System.out.println("\nResuming MyThread again.");
25 mt.myResume();
26 Thread.sleep(3000);
27
28 System.out.println("\nStopping MyThread.");
29 mt.myStop();
30
31 } catch (InterruptedException e) {
32 System.out.println("Main thread Interrupted");
33 }
34 }
35 }
36
37 class MyThread implements Runnable {
38 Thread thrd;
39 private volatile boolean suspended;//暂停
40 private volatile boolean stopped;
41
42 MyThread(String name){
43 thrd = new Thread(this, name);
44 suspended = false;
45 stopped = false;
46 thrd.start();
47 }
48 @Override
49 public void run() {
50 System.out.println(thrd.getName() + " starting.");
51 try {
52 for (int i = 0; i < 1000; i++) {
53 if (stopped) {
54 System.out.println("Stopping begin.(1):");
55 }
56 System.out.print(".");
57 Thread.sleep(300);
58 if (stopped) {
59 System.out.println("Stopping begin.(2):");
60 }
61 synchronized (this) {
62 while(suspended) wait();
63
64 //如果thread要停止,跳出循环,线程结束。
65 if (stopped) {
66 break;
67 }
68 }
69 }
70 } catch (InterruptedException e) {
71 System.out.println(thrd.getName() + " interrupted.");
72 }
73 System.out.println("\n" + thrd.getName() + " exiting.");
74 }
75
76 //调用方法停止线程
77 synchronized void myStop() {
78 stopped = true;
79
80 suspended = false;
81 notify();
82 }
83
84 //暂停线程
85 synchronized void mySuspend() {
86 suspended = true;
87 }
88
89 //恢复线程
90 synchronized void myResume(){
91 suspended = false;
92 notify();
93 }
94 }

结果:

MyThread starting.
..........
Suspending MyThread.

Resuming MyThread.
...........
Suspending MyThread again.

Resuming MyThread again.
...........
Stopping MyThread.
Stopping begin.(2):

MyThread exiting.

多线程编程<三>的更多相关文章

  1. [置顶] 曙光到来,我的新书《Android进阶之光》已出版

    独立博客版本请点击这里 由来 2016年我开始建立了自己的知识体系,所有的文章都是围绕着这个体系来写,随着这个体系的慢慢成长,开始有很多出版社联系我写书,因为比较看好电子工业出版社,就顺理成章的开始了 ...

  2. 《Android进阶之光》--注解与依赖注入框架

    No1: 标准注解: 1)@Override:覆写 2)@Deprecated:过时 3)@SuppressWarnings:取消警告 4)@SafeVarargs:申明使用了可变长度参数的方法 No ...

  3. 《Android进阶之光》--Material Design

    接上篇<Android进阶之光>--Android新特性 No1: 组件: 1)底部工作条-Bottom Sheets 2)卡片-Cards 3)提示框-Dialogs 4)菜单-Menu ...

  4. Android中的多线程编程(一)附源代码

    Android中多线程编程:Handler类.Runnable类.Thread类之概念分析 1.Handler类: Handler是谷歌封装的一种机制:能够用来更新UI以及消息的发送和处理.Handl ...

  5. 《Android进阶之光》--多线程编程

    No1: 实现多线程的3中方法 1)继承Thread,重写run()方法 2)实现Runnable接口,并实现该接口的run()方法 3)实现Callable接口,重写call()方法 public ...

  6. 《Android进阶之光》--网络编程与网络框架

    No1: Volley源码分析: Volley.newRequestQueue-> RequestQueue.start()-> CacheDispatcher.start()->C ...

  7. Android进阶(九)APP编程感想

    从初识Android到现在,在不断做APP(二维码.条形码扫描,彩票购买,火车票余票查询)的过程中,自己学会了很多东西.找时间整理了一下,总结如下: 其中,对于前两个APP,自己都是在他人已完成的基础 ...

  8. Android中的多线程编程

    问题 Android的UI也是线程不安全的,如果要更新应用程序里的UI元素,必须在主线程中进行,否则就会抛异常.比如用一个Button的onClick函数去更新界面上的元素,就会得到一个CalledF ...

  9. 《Android进阶之光》--Android新特性

    Android 5.0新特性 1)全新的Material Design设计风格 2)支持多种设备 3)全新的通知中心设计--按照优先级显示 4)支持64位ART虚拟机 5)多任务视窗Overview ...

  10. Android进阶之光-第1章-Android新特性-读书笔记

    第 1 章 Android 新特性 1.1 Android 5.0 新特性 1.1.1 Android 5.0 主要新特性 1. 全新的 Material Design 新风格 Material De ...

随机推荐

  1. python + mysql 实现表更新数据

    实例如下: import pymysqldef Update_Set(): #打开数据库链接 db = pymysql.connect("localhost","root ...

  2. vue3如何编写挂载DOM的插件

    vue3 跟 vue2 相比,多了一个 app 的概念,vue3 项目的创建也变成了 // main.jsimport { createApp } from 'vue' import App from ...

  3. P4774-屠龙勇士-扩展中国剩余定理

    屠龙勇士 很久很久以前,巨龙突然出现,带来了灾难带走公主又消失不见.王国十分危险,世间谁最勇敢,一位英雄出现-- 学习于该大佬博客 那么你就是这位英雄,不过不同的是,你面对的是一群巨龙,虽然巨龙都不会 ...

  4. python中的abstractmethod

    # -*- coding: utf-8 -*- from abc import ABC ,abstractclassmethod from collections import namedtuple ...

  5. Python基础之subprocess

    前言 subprocess这个函数很好用,类似于控制台执行,功能很多,今天先介绍subprocess调用exe,并行调用两个或两个以上的exe. Subprocess调用exe 调用exe有几种方式, ...

  6. Server-Speaks-First 有点坑,Linkerd 2.10 中的协议检测和不透明端口

    协议检测(Protocol detection),顾名思义,允许 Linkerd 自动检测 TCP 连接中使用的协议. Linkerd 的设计原则之一是"just work",协议 ...

  7. vue传值 ---- >> 父传子,props()

    父组件:     1 <template> 2     <div class="comment"> 3         <div>comment ...

  8. 基于CAS实现SSO单点登录

    1. 概述 1.1. 什么是SSO? 单点登录( Single Sign-On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决方案之一, SSO 使得在多个应用系统中,用户只需要 登录一 ...

  9. Vue 脚手架学习

    首先就是安装脚手架 npm install @vue/cil -g 全局安装 在这里我遇到一个问题:安装不了脚手架,报错显示: 通过苦逼的查找原因就是 以前使用的taobao镜像 导致的,删除镜像换成 ...

  10. Android 已发行多年,移动 App 已经趋近饱和,那么 Android 开发还会有那么吃香吗?

    一.关于Android的前景 不断地也听见很多人在谈做Android是否还有前途.Android研发在走下坡路了.Android的工作太难找了.Android是不是已经凉了...... 对于这些其实我 ...