java多线程之yield,join,wait,sleep的区别
Java多线程之yield,join,wait,sleep的区别
Java多线程中,经常会遇到yield,join,wait和sleep方法。容易混淆他们的功能及作用。自己仔细研究了下,他们主要的区别是在cpu的占用和共享资源的锁上面。
wait:是继承自Object的方法,当前线程调用wait方法,是在告诉别的线程,我需要等待了,既会释放cpu,也会释放共享资源的锁,进入挂起状态。wait必须在synchronized代码块内部执行,因为wait需要获得共享资源的锁并且释放锁。需要notify/notifyAll来唤醒。wait主要和sleep进行区别,
join:Thread的非静态方法。底层用了wait方法。假如现在有两个线程,main线程和t线程.main线程里调用t.join.那么这时,main会取得线程对象t的锁,然后main线程 wait,释放cpu,t线程执行,
直到t线程执行完后,main线程继续执行。
jdk中join的源代码:
/**
* Waits at most {@code millis} milliseconds for this thread to
* die. A timeout of {@code 0} means to wait forever.
*
* <p> This implementation uses a loop of {@code this.wait} calls
* conditioned on {@code this.isAlive}. As a thread terminates the
* {@code this.notifyAll} method is invoked. It is recommended that
* applications not use {@code wait}, {@code notify}, or
* {@code notifyAll} on {@code Thread} instances.
*
* @param millis
* the time to wait in milliseconds
*
* @throws IllegalArgumentException
* if the value of {@code millis} is negative
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
/**
* Tests if this thread is alive. A thread is alive if it has
* been started and has not yet died.
*
* @return <code>true</code> if this thread is alive;
* <code>false</code> otherwise.
*/
public final native boolean isAlive();
通过源代码,可以看出,如果t线程生成了,但是还未start,那么join方法是没有效果的。
sleep:是Thread的静态方法,会使当前线程释放cpu,但不会释放锁资源,可以理解为只和cpu有关,不涉及锁资源。涉及锁资源的,是wait,join方法。
yield:也是Thread的静态方法,和sleep方法类似,会使当前线程释放cpu,但不会释放锁资源。和sleep不同的是,sleep必须设置时间,但是yield不能设置时间,时间值是随机的
java多线程之yield,join,wait,sleep的区别的更多相关文章
- java 多线程之:join() 方法
join()介绍 join() 定义在java.lang.Thread中. join() 的作用:让"主线程"等待"子线程"结束之后才能继续运行.
- java多线程之yield()方法详解
yiled()方法的作用是放弃当前CPU的资源,将资源让给其它线程,但放弃的时间不确定,有可能刚刚放弃,又马上获得了CPU时间片.下面看一个小例子,看一下具体效果. public stati ...
- Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术
package mythread; public class CountOperate extends Thread{ public CountOperate(){ System.out.prin ...
- JAVA多线程之wait/notify
本文主要学习JAVA多线程中的 wait()方法 与 notify()/notifyAll()方法的用法. ①wait() 与 notify/notifyAll 方法必须在同步代码块中使用 ②wait ...
- Java多线程之ConcurrentSkipListMap深入分析(转)
Java多线程之ConcurrentSkipListMap深入分析 一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下, ...
- JAVA多线程之volatile 与 synchronized 的比较
一,volatile关键字的可见性 要想理解volatile关键字,得先了解下JAVA的内存模型,Java内存模型的抽象示意图如下: 从图中可以看出: ①每个线程都有一个自己的本地内存空间--线程栈空 ...
- Java多线程之Runnable与Thread
Java多线程之Thread与Runnable 一.Thread VS Runnable 在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类和 ...
- JAVA多线程之UncaughtExceptionHandler——处理非正常的线程中止
JAVA多线程之UncaughtExceptionHandler——处理非正常的线程中止 背景 当单线程的程序发生一个未捕获的异常时我们可以采用try....catch进行异常的捕获,但是在多线程环境 ...
- java多线程之wait和notify协作,生产者和消费者
这篇直接贴代码了 package cn.javaBase.study_thread1; class Source { public static int num = 0; //假设这是馒头的数量 } ...
随机推荐
- Zookeeper 3.5启动时 8080端口被占用
今天闲来无事,学习Zookeeper,下载了Zookeeper的最新版本3.5.启动以后显示: ZooKeeper JMX enabled by default Using config: /opt/ ...
- SQL Prompt snippets
SQL Prompt snippets https://github.com/gvohra/sqlpromptsnippets
- WebForm内置对象:Session、Cookie,登录和状态保持
1.Request -获取请求对象 string s =Request["key"]; 2.Response - 响应请求对象 Response.Redirect(" ...
- url的参数解析成key-value
function urlController(url) { var _url = url.split("?")[1]; if(!_url){ return {}; } var wi ...
- 《数据仓库ETL工具箱》读书笔记
在本书中,你将学习到以下内容: 规划&设计你的ETL系统 从多种可能的架构中选出最合适的 对实施过程进行管理 管理日常的操作 为ETL过程建立开发/测试/生产环境 理解不同的后台数 ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- Centos7 升级 Ruby
Centos7通过yum 安装的Ruby 是2.0版本.版本较低,需要升级到2.5以上版本. #yum 安装ruby yum install ruby #查看ruby版本 ruby -v 以下开始升级 ...
- Shell 常用技巧
Shell 常用技巧 echo $RANDOM | cksum | cut -c - openssl rand -base64 | cksum | cut -c - date +%N | cut -c ...
- webpack 几个基本打包扩展项的安装命令
网速比较慢的童鞋,装包时请准备好花生瓜子 webpack 本身只能打包 JavaScript 模块,但是它可以通过拓展打包比如静态资源文件.less.sass.typescript 等,还可以通过 b ...
- 数据库分区分表(sql、mysql)
http://blog.csdn.net/lgb934/article/details/8662956 http://www.2cto.com/database/201503/380348.html ...