从头认识多线程-1.9 迫使线程停止的方法-return法
这一章节我们来讨论一下还有一种停止线程的方法-return
1.在主线程上面return,是把全部在执行的线程都停掉
package com.ray.deepintothread.ch01.topic_9;
public class StopByReturn {
public static void main(String[] args) throws InterruptedException {
ThreadFive threadFive = new ThreadFive();
Thread thread1 = new Thread(threadFive);
thread1.start();
Thread thread2 = new Thread(threadFive);
thread2.start();
Thread.sleep(200);
return;
}
}
class ThreadFive implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " begin");
try {
System.out.println(Thread.currentThread().getName() + " working");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " exit");
}
}
}
输出:
Thread-1 begin
Thread-1 working
Thread-0 begin
Thread-0 working
2.在当前线程return,仅仅是单纯的停止当前线程
package com.ray.deepintothread.ch01.topic_9;
public class StopByReturn2 {
public static void main(String[] args) throws InterruptedException {
ThreadOne threadOne = new ThreadOne();
threadOne.start();
Thread.sleep(200);
threadOne.interrupt();
}
}
class ThreadOne extends Thread {
@Override
public void run() {
while (true) {
if (this.isInterrupted()) {
System.out.println("return");
return;
}
}
}
}
输出:
return
package com.ray.deepintothread.ch01.topic_9;
import java.util.ArrayList;
public class StopByReturn3 {
public static void main(String[] args) throws InterruptedException {
ThreadOne threadTwo = new ThreadOne();
ArrayList<Thread> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Thread thread = new Thread(threadTwo);
thread.start();
list.add(thread);
}
Thread.sleep(20);
list.get(0).interrupt();
}
}
class ThreadTwo implements Runnable {
@Override
public void run() {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("Thread name:" + Thread.currentThread().getName() + " return");
return;
}
}
}
}
输出:
Thread name:Thread-0 return
(然后其它线程一直在执行)
总结:这一章节我们讨论了一下return的两个方面,大家须要特别注意一点的就是return在main里面的运用。
从头认识多线程-1.9 迫使线程停止的方法-return法的更多相关文章
- 从头认识多线程-1.8 迫使线程停止的方法-暴力Stop方法
这一章节我们来讨论一下暴力Stop方法. 1.使用样例 package com.ray.deepintothread.ch01.topic_8; public class StopByStopMeth ...
- 零基础学习java------day18------properties集合,多线程(线程和进程,多线程的实现,线程中的方法,线程的声明周期,线程安全问题,wait/notify.notifyAll,死锁,线程池),
1.Properties集合 1.1 概述: Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串 一个属性列表可包含另 ...
- Thread的中断机制(interrupt),循环线程停止的方法
一.中断原理 中断线程 线程的thread.interrupt()方法是中断线程,将会设置该线程的中断状态位,即设置为true,中断的结果线程是死亡.还是等待新的任务或是继续运行至下一步,就取决于这个 ...
- Java多线程系列三——实现线程同步的方法
两种实现线程同步的方法 方法 特性 synchronized 不需要显式地加解锁,易实现 ReentrantLock 需要显式地加解锁,灵活性更好,性能更优秀,结合Condition可实现多种条件锁 ...
- Java线程停止interrupt()方法
程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决,将导致意外的行为以及细微的.难以发现的错误.在本篇文章中,我们针对这些难题之一:如何中断一个正在运行的线程. 中 ...
- java多线线程停止正确方法
//军队线程 //模拟作战双方的行为 public class ArmyRunnable implements Runnable { //volatile保证了线程可以正确的读取其他线程写入的值 // ...
- 多线程---其他方法 停止线程、守护线程、join方法
第三方停止线程: 原来是stop(),因为该方法有些问题,所以被interrupt()方法取代,它的用途跟机制是 当没有指定的方式让冻结的线程恢复到运行状态时,这时需要对冻结进行清除,强制让线程恢复到 ...
- java多线程总结五:线程池的原理及实现
1.线程池简介: 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 创 ...
- C#多线程实践——锁和线程安全
锁实现互斥的访问,用于确保在同一时刻只有一个线程可以进入特殊的代码片段,考虑下面的类: class ThreadUnsafe { static int val1, val2; static void ...
随机推荐
- Java并发(十四):并发工具类——CountDownLatch
先做总结: 1.CountDownLatch 是什么? CountDownLatch 允许一个或多个线程等待其他线程(不一定是线程,某个操作)完成之后再执行. CountDownLatch的构造函数接 ...
- @SuppressWarning 抑制警告注解
@SuppressWarning 抑制警告注解 Java.lang.SuppressWarnings 是 J2SE5.0中标准的Annotation 之一. 可以标注在类,字段,方法,参数,构造方法, ...
- python开发_pprint()
python中的pprint.pprint(),类似于print() 下面是我做的demo: #python pprint '''python API中提供的Sample''' import json ...
- python开发_类型转换convert
在python的开发过程中,难免会遇到类型转换,这里给出常见的类型转换demo: int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float ...
- hust 1385 islands 并查集+搜索
islands Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/problem/show/1385 Descri ...
- Unity 的一些特性
using System; using UnityEngine; using UnityEditor; using UnityEngine.Serialization; using Random = ...
- HTML5 book响应式翻页效果
翻页,HTML5源码下载,HTML5响应式翻页效果,鼠标移到右上角会看到翻页效果,需要鼠标拖动后翻页,支持ie9+,html5浏览器. 单页和双页. 自动播放和暂停. 点击左右翻页. 鼠标点击左右页面 ...
- FAQ:领域服务和应用服务的职责是什么?
问答部分 问: 领域服务的职责是什么? 答: 夸聚合实例业务逻辑. 没办法合理放到实体中的其它业务逻辑. 问: 领域服务的设计原则是什么? 答: 用来组织业务逻辑. 面向业务逻辑. 细粒度. 内部视图 ...
- MNI模板和Talairach 模板的对比
The MNI brain and the Talairach atlas SPM 96 and later use standard brains from the Montreal Neurolo ...
- RxJava 中文文档
https://mcxiaoke.gitbooks.io/rxdocs/content/Subject.html