博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA 线程基本知识汇总--Join yield
阅读量:7143 次
发布时间:2019-06-29

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

hot3.png

Join 让执行这个方法的线程插队 ,让当前县城执行完再执行别的线程

package org.famous.unyielding.current.jooin;public class ThreadJoinClient {	public static void main(String[] args) {		Thread t = new Thread(new Runnable() {			@Override			public void run() {				System.err.println("Hello World");			}		});		try {			t.join();		} catch (InterruptedException e) {			e.printStackTrace();		}		t.start();		System.err.println("main thread");	}}

加Join 和不加Join的结果就是hello World 输出的顺序不一致。加了Join那么Hello World 会优先执行

Join(Long xx) Sleep(Long xx)的区别 。Join会释放锁,但是Sleep不会

首先验证Join(Long xx)的方法 第二证明Join锁机制:

package org.famous.unyielding.current.jooin;public class ThreadJoinClient {	public static void main(String[] args) {		Thread t = new Thread(new Runnable() {			@Override			public void run() {				try {					Thread.sleep(4000);				} catch (InterruptedException e) {					e.printStackTrace();				}				System.err.println("Hello World");			}		});		t.start();		try {			t.join(2000);		} catch (InterruptedException e) {			e.printStackTrace();		}		System.err.println("main thread");	}}

必须先Start再Join 否则无效。!这点详细见join的源码:

  1. if (millis == 0) {  

  2.             while (isAlive()) {  

  3.                 wait(0);  

  4.             }  

  5.         } else {  

join的锁机制:

上面的代码中得看是谁调用了join 是主线程 所以停止的是主线程。

本质问题:join(1000)和sleep(1000)的区别是什么。让子线程Join 或者让主线程sleep 本质上都可以达到我们的要求。只要我们预估好时间。

但是如果不加时间呢。还是为了验证锁的问题。

就是join 会不会让调用它的线程释放锁。

1.join 的参数并不是一直等待到指定的时间,如果线程提前运行完成之后还是会提前结束等待的

2.

public class MyTest {	public static void main(String[] args) {		SonRun sonRun = new SonRun();		Thread thread = new Thread(new JoinRunn(sonRun));		thread.start();		Thread thread2 = new Thread(new ThreeRrun(sonRun));		thread2.start();	}}// 我调用join 会释放锁吗class JoinRunn implements Runnable {	private SonRun sonrun;	public JoinRunn(SonRun sonrun) {		super();		this.sonrun = sonrun;	}	@Override	public void run() {		synchronized (sonrun) {			System.err.println("join run get the sonrun lock");			try {				sonrun.start();				/***  zhongdian **/				// sonrun.join(3000);				Thread.sleep(3000);				System.err.println("join run will execute after SonRun");				for (int i = 0; i < Integer.MAX_VALUE; i++) {					String newString = new String();					Math.random();				}			} catch (InterruptedException e) {				e.printStackTrace();			}		}	}}class SonRun extends Thread {	@Override	public void run() {		Long begin = System.currentTimeMillis();		try {			Thread.sleep(2000);		} catch (InterruptedException e) {			e.printStackTrace();		}		Long end = System.currentTimeMillis();		System.err.println(begin - end);	}}class ThreeRrun implements Runnable {	private SonRun sonrun;	public ThreeRrun(SonRun sonrun) {		this.sonrun = sonrun;	}	@Override	public void run() {		synchronized (sonrun) {			System.err.println("what the time shold i fucked be execute");		}	}}

这个代码的运行结果在JoinRunn的

// sonrun.join(3000);

Thread.sleep(3000);

分别运行结果是不同的。所以得警惕Join 是会释放锁这个问题的。

join() If any executing thread t1 calls join() on t2 i.e; t2.join() immediately t1 will enter into waiting state until t2 completes its execution.  t1调用了t2的join方法,那么t1就会先wait 直到时间结束或者t1运行结束。既然是wait,那么同步的或者加锁的究竟是哪个地方??

  1. yield()

  2. join()

  3. sleep()

  4. yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent.

  5. sleep() Based on our requirement we can make a thread to be in sleeping state for a specified period of time (hope not much explanation required for our favorite method).

下面关于Yield的

package org.famous.unyielding.current.jooin;public class YieldThreadClient {	public static void main(String[] args) {		Thread thread1 = new Thread(new YieldRunn());		Thread thread2 = new Thread(new YieldRunn());		thread1.start();		thread2.start();	}}class YieldRunn implements Runnable {	@Override	public void run() {		for (int i = 0; i < 130; i++) {			System.err.println(i);			if (i == 30) {				Thread.yield();			}		}	}}

转载于:https://my.oschina.net/payzheng/blog/513171

你可能感兴趣的文章
如何在 Web 关闭页面时发送 Ajax 请求
查看>>
vue2 基础学习01 ( 核心最基本的功能)
查看>>
前端_JavaScript_Method
查看>>
LeetCode28.实现strStr() JavaScript
查看>>
由奥迪车灯想到的前端动画
查看>>
C++编译器优化
查看>>
在 Vue 中是使用插槽
查看>>
币安布局去中心化交易所,原来是因为这三个原因!
查看>>
nodeJS贪吃蛇
查看>>
为什么S/4HANA的销售订单创建会触发生产订单的创建
查看>>
php原生数据库分页
查看>>
92. Reverse Linked List II
查看>>
js组合模式和寄生组合模式的区别研究
查看>>
Bye, 2018; Hi, 2019
查看>>
谈谈super(props) 的重要性
查看>>
LeetCode22.括号生成 JavaScript
查看>>
cookie localstorage sessionStorage
查看>>
某数加密的流程与原理简析
查看>>
《前端十年心路》书稿规划
查看>>
Java虚拟机规范(目录)
查看>>