一、join()方法(插队)
/**
* Waits for this thread to die.
*
* <p> An invocation of this method behaves in exactly the same
* way as the invocation
*
* <blockquote>
* {@linkplain #join(long) join}{@code (0)}
* </blockquote>
*
* @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 void join() throws InterruptedException {
join(0);
}
我们可以看一下join方法注释,非常简单的一句话:Waits for this thread to die.一直等到当前线程死亡,也就是说线程调用join方法后,其他线程会一直等待这个线程运行完,才能继续运行,这也就很好的解释了Happen-before模型里面为什么join能解决有序性可见性呢,因为当前线程运行完后,会把工作内存中的变量更新到主内存中。
我们看一个例子:
创建个线程,后面用的到
public class ThreadDemo extends Thread {
@Override
public void run() {
System.out.println("这是继承Thread类创建线程");
}
}
public class JoinDemo {
public static void main(String[] args) throws InterruptedException {
ThreadDemo t1 = new ThreadDemo();
t1.start();;
// t1.join();
System.out.println("主线程");
}
}
看看调用join前效果:
主线程
这是继承Thread类创建线程
看看调用join后效果
这是继承Thread类创建线程
主线程
结果发现,调用join方法的线程先执行。
注意join方法可以设置时间,如 join(1000)
二、yield方法(让步)
/**
* A hint to the scheduler that the current thread is willing to yield
* its current use of a processor. The scheduler is free to ignore this
* hint.
*
* <p> Yield is a heuristic attempt to improve relative progression
* between threads that would otherwise over-utilise a CPU. Its use
* should be combined with detailed profiling and benchmarking to
* ensure that it actually has the desired effect.
*
* <p> It is rarely appropriate to use this method. It may be useful
* for debugging or testing purposes, where it may help to reproduce
* bugs due to race conditions. It may also be useful when designing
* concurrency control constructs such as the ones in the
* {@link java.util.concurrent.locks} package.
*/
public static native void yield();
我们可以看出yield()方法可以让出当前运行线程,让同优先级的线程先执行。
我们通过下面的一个示例演示效果
public class YieldDemo {
public static void main(String[] args) throws InterruptedException {
ThreadDemo t1 = new ThreadDemo();
t1.start();
// t1.yield();
System.out.println("主线程");
}
}
打印效果:
主线程
这是继承Thread类创建线程
调用yield方法后,打印效果:
主线程
这是继承Thread类创建线程
但是多次运行后,会出现这样一种结果:
这是继承Thread类创建线程
主线程
为什么会出现不同的结果呢?
yield()应该做的是让当前运行线程回到可运行状态,以允许具有相同优先级的其他线程获得运行机会。因此,使用yield()的目的是让相同优先级的线程之间能适当的轮转执行。但是,实际中无法保证yield()达到让步目的,因为让步的线程还有可能被线程调度程序再次选中。
三、sleep(long millis)方法
/**
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds, subject to
* the precision and accuracy of system timers and schedulers. The thread
* does not lose ownership of any monitors.
*
* @param millis
* the length of time to sleep 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 static native void sleep(long millis) throws InterruptedException;
看方法的注释,我们可以知道:
使当前执行的线程休眠(暂时停止执行)指定的毫秒数,这取决于系统计时器和调度器的精度和准确性。线程不会失去任何监视器的所有权。
通俗讲就是让线程暂停一会,不会释放锁
面试的时候经常会问到sleep和wait(线程通信)区别,通过这个我们简单总结下:
1.sleep是Thred类的方法,wait是Object类的方法
2.sleep不会释放锁,wait会释放锁,需要notify唤醒
注意:对于sleep、wait、join都会抛出InterruptedException,为什么呢?
参考线程的终止、复位来找到答案吧
本文是综合自己的认识和参考各类资料(书本及网上资料)编写,若有侵权请联系作者,所有内容仅代表个人认知观点,如有错误,欢迎校正; 邮箱:1354518382@qq.com 博客地址:https://blog.csdn.net/qq_35576976/