Thread.yeild方法详解
从原理上讲其实Thread.yeild方法其实只是给线程调度机制一个暗示:我的任务处理的差不多了,可以让给相同优先级的线程CPU资源了;不过确实只是一个暗示,没有任何机制保证它的建议将被采纳;
看一个例子就知道了;
public class LiftOff implements Runnable {
protected int countDown = 5;
private static int taskCount = 0;
private final int id = taskCount++;
public LiftOff() {
}
public LiftOff(int countDown) {
this.countDown = countDown;
}
public String status() {
// System.out.println(Thread.currentThread().getName());
return Thread.currentThread().getName() + "#" + id + "(" + (countDown > 0 ? countDown + ")" : "Liftoff!" + ")");
}
@Override
public void run() {
// TODO Auto-generated method stub
/*
* --countDown:先减去,再赋值
* countDown--先赋值,再减去
*/
while (countDown-- > 0) {
System.out.println(status());
Thread.yield();// 让步【表示我的任务处理的差不多了,可以让步给其他线程资源了】;CPU资源给其他线程使用;t.join当前线程挂起,直到t线程调用结束后切回
/*
* try {
* TimeUnit.MILLISECONDS.sleep(100);
* } catch (InterruptedException e) {
* // TODO Auto-generated catch block
* // e.printStackTrace();
* System.err.println("异常终止...");
* }
*/
}
}
public class ExecuteMethodPool {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();// 无界队列:将为每个任务创建一个线程
// ExecutorService executorService = Executors.newFixedThreadPool(2);// 有限线程数队列
// ExecutorService executorService = Executors.newSingleThreadExecutor();// 有限线程数队列
for (int i = 0; i < 3; i++) {
executorService.execute(new LiftOff());
}
executorService.shutdown();
}
}
响应结果打印:
p.p1 { margin: 0; font: 12px Menlo }
pool-1-thread-2#1(4)
pool-1-thread-1#0(4)
pool-1-thread-3#2(4)
pool-1-thread-2#1(3)
pool-1-thread-3#2(3)
pool-1-thread-3#2(2)
pool-1-thread-1#0(3)
pool-1-thread-2#1(2)
pool-1-thread-3#2(1)
pool-1-thread-1#0(2)
pool-1-thread-2#1(1)
pool-1-thread-3#2(Liftoff!)
pool-1-thread-1#0(1)
pool-1-thread-2#1(Liftoff!)
pool-1-thread-1#0(Liftoff!)
以上标注的两个一样的线程先后执行 并没有遵守yeild给线程调度的建议
Thread.yeild方法详解的更多相关文章
- JAVA THREAD.JOIN方法详解
一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...
- BigDecimal 使用方法详解
BigDecimal 使用方法详解 博客分类: java基础 bigdecimalmultiplyadddivide BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (sca ...
- Java中的main()方法详解
在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方法有很大的不同,比如方法的名字必须是main,方法必须是 ...
- $.ajax()方法详解 ajax之async属性 【原创】详细案例解剖——浅谈Redis缓存的常用5种方式(String,Hash,List,set,SetSorted )
$.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...
- Java AtomicInteger类的使用方法详解_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 首先看两段代码,一段是Integer的,一段是AtomicInteger的,为以下: public class Samp ...
- java多线程并发(二)--线程的生命周期及方法详解
上篇随笔介绍了线程的相关基础知识以及新启线程的几种方法,本片将继续介绍线程的生命周期及方法详解. 一.线程的生命周期 在Thread代码中,线程的状态被分为6种 public enum State { ...
- 并发编程(六)Object类中线程相关的方法详解
一.notify() 作用:唤醒一个正在等待该线程的锁的线程 PS : 唤醒的线程不会立即执行,它会与其他线程一起,争夺资源 /** * Object类的notify()和notifyAll()方法详 ...
- C#多线程详解(一) Thread.Join()的详解
bicabo C#多线程详解(一) Thread.Join()的详解 什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程 ...
- AsList()方法详解
AsList()方法详解 在Java中,我们应该如何将一个数组array转换为一个List列表并赋初始值?首先想到的肯定是利用List自带的add()方法,先new一个List对象,再使用add()方 ...
随机推荐
- 排序--HeapSort 堆排序
堆 排 序 堆排序.就是通过堆结构来排序.可以看之前写的http://www.cnblogs.com/robsann/p/7521812.html .关于堆的结构 堆排序先要使结构堆有序.所以要先使所 ...
- 第7.16节 案例详解:Python中classmethod定义的类方法
第7.16节 案例详解:Python中classmethod定义的类方法 上节介绍了类方法定义的语法以及各种使用的场景,本节结合上节的知识具体举例说明相关内容. 一. 案例说明 本节定义的一个 ...
- 第8.12节 Python类中使用__dict__定义实例变量和方法
上节介绍了使用实例的__dict__查看实例的自定义属性,其实还可以直接使用__dict__定义实例变量和实例方法. 一. 使用__dict__定义实例变量 语法: 对象名. dict[属性名] = ...
- JAVA课堂作业(2019.10.14)
一. (1)代码 package class20191014; import java.util.Scanner; public class ClassHomework { public static ...
- VMware 12 与Centos 7建立共享文件夹 && vmware 15 pro过期激活秘钥
1 vmware 15pro评估过期 网上找到的激活秘钥,激活密钥: YG5H2-ANZ0H-M8ERY-TXZZZ-YKRV8(亲试有效)UG5J2-0ME12-M89WY-NPWXX-WQH88U ...
- 百度前端技术学院-基础-day20-21
第二十到第二十一天:让你和页面对话 task1 控制元素的显示及隐藏 实现以下功能: 当用户选择了 School 的单选框时,显示 School 的下拉选项,隐藏 Company 的下拉选项 当用户选 ...
- 差分约束系统——POJ1275
之前做过差分,但是没做过差分约束系统. 正好在学军机房听课讲到这道题,就顺带学了一下. 其实...就是列不等式组然后建图 作为蒟蒻,当然是不会加二分优化的啦...但是poj上还是94ms跑过了qwq ...
- tornado 作业 自定义模板 UIMethod以UIModule
自定义uimodule s3.py import tornado.ioloop import tornado.web import UIMethod as mt class MainHandler(t ...
- js中的bind、apply、call、callee、caller的区别
1.bind.apply与call的区别与使用 相同点:2者是函数原型的一个方法,因此调用者都必须是函数,第1个参数都是对象.作用是,用另一个对象替换当前对象,另一对象也即是你传的第一个参数.通常用于 ...
- python绘折线图
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt #X轴,Y轴数据 y = [0.3,0.4,2,5 ...