java线程之——sleep()与wait()的区别
sleep()是Thread的方法,wait()是Object的方法
如果线程进入了同步锁,sleep不会释放对象锁,wait会释放对象锁
sleep的作用就是让正在执行的线程主动让出CPU,给其它线程获得CPU的机会,在sleep指定的时间之后,CPU才会回到这个线程上继续往下执行,当线程进入了同步锁时,当别的线程也需要被加锁的资源时,sleep方法即使让出了CPU,别的线程也无法执行,因为无法获得锁。
wait方法是指在一个已经进入了同步锁的线程内,让自己暂时让出同步锁给别的线程用,只有等其他线程调用了notify或notifyAll方法后,才能去获得同步锁继续执行,需要注意的是,notify并不会释放锁,只是告诉调用过wait方法的线程可以去争取锁了,而不是马上得到锁。
此外在使用wait,notify,notifyAll方法的时候,要注意只能使用对象锁的持有者,即被封锁的对象来调用,不然会出IllegalMonitorStateException异常。
package javaBase;
/**
* sleep()和wait()的区别
* sleep()是Thread的方法,wait()是Object的方法
* sleep不会释放封锁的资源,wait会释放封锁的资源
* @author cnxno1
*
*/
public class JB_047_SleepAndWait {
public static void main(String[] args) {
Thread thread1 = new Thread(new Thread1());
Thread thread2 = new Thread(new Thread2());
thread1.start(); try {
System.out.println("延迟5秒启动线程2");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} thread2.start();
}
}
/**
* 公共资源类
* @author cnxno1
*
*/
class Resource{
public static String resource = "public resource";
}
/**
* 调用wait方法的线程
* @author cnxno1
*
*/
class Thread1 implements Runnable{
@Override
public void run(){
synchronized(Resource.resource){
System.out.println("this is thread1 , i hava the "+Resource.resource);
System.out.println("this is thread1 , i'm waiting the "+Resource.resource);
try{
Resource.resource.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("this is thread1, i will going on...");
System.out.println("this is the end of thread1.");
}
}
} /**
* 调用sleep方法的线程
* @author cnxno1
*
*/
class Thread2 implements Runnable{
@Override
public void run(){
synchronized(Resource.resource){
System.out.println("this is thread2 , i'm notifyAll thread...");
Resource.resource.notifyAll();
System.out.println("this is thread2 , i will sleep 10 seconds...");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("this is thread2, i will going on...");
System.out.println("this is the end of thread2.");
}
}
}
运行的结果如下:
延迟5秒启动线程2
this is thread1 , i hava the public resource
this is thread1 , i'm waiting the public resource
this is thread2 , i'm notifyAll thread...
this is thread2 , i will sleep 10 seconds...
this is thread2, i will going on...
this is the end of thread2.
this is thread1, i will going on...
this is the end of thread1.
可以看到thread1在调用了wait方法之后,thread2获得了同步锁,但是在thread2调用了sleep方法后,thread1并没有获得同步锁执行下去,而是在thread2 sleep了10秒执行完之后才获得了Resource.resource的锁得以执行完成。
java线程之——sleep()与wait()的区别的更多相关文章
- java线程中的sleep和wait区别
面试题:java线程中sleep和wait的区别以及其资 ...
- java 线程同步 原理 sleep和wait区别
java线程同步的原理java会为每个Object对象分配一个monitor, 当某个对象(实例)的同步方法(synchronized methods)被多个线程调用时,该对象的monitor将负责处 ...
- JAVA线程池shutdown和shutdownNow的区别
一.区别介绍 shutDown() 当线程池调用该方法时,线程池的状态则立刻变成SHUTDOWN状态.此时,则不能再往线程池中添加任何任务,否则将会抛出RejectedExecutionExcept ...
- 【转】java线程系列---Runnable和Thread的区别
在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...
- java线程interrupt、interrupted 、isInterrupted区别
前言 在分析interrupt之前,应该先了解java里线程有5种状态,其中有一个阻塞状态,interrupt和阻塞有关. interrupt() 方法 作用于要中断的那个线程. interrupt( ...
- java线程(上)Thread和Runnable的区别
首先讲一下进程和线程的区别: 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程. 线程:同一类线程共享代码和数据空间,每个线程有独立的运行栈 ...
- java线程系列---Runnable和Thread的区别 (转载)
转自:http://blog.csdn.net/wwww1988600/article/details/7309070 在java中可有两种方式实现多线程,一种是继承 Thread类,一种是实现Run ...
- Java线程 - sleep()和wait()方法的区别, 线程阻塞BLOCKED和等待WAITING的区别
一. sleep()和wait()方法的区别 sleep()方法 sleep()方法是Thread类的方法,通过其定义可知是个native方法,在指定的时间内阻塞线程的执行.而且从其注释中可知,并不会 ...
- java线程中start和run的区别
public class Test1 extends Thread { @Override public void run() { while (true) { System.out.println( ...
随机推荐
- ios The App Life Cycle
先推荐ios 必读文章 App Programming Guide for iOS ,请在苹果官网搜索,并仔细阅读所有内容 State Description Not running The app ...
- iOS 串行网络请求。。。待研究
nsurlsession 和 nsurlconnection 能实现吗? 手动实现的关键点在哪里? 我这里说的串行网络请求,指的是第一个网络请求不返回数据,第二个网络请求就不能开始. AFNetwor ...
- Qt 对话框显示控制按钮
在对话框窗体构造函数加入 SystemDialog::SystemDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SystemDialog) ...
- ItemsSource绑定后ScrollViewer不复位
ItemsSource绑定后ScrollViewer不复位 ItemsSource绑定后ScrollViewer不复位,有的时候我们需要这一效果,但大多数情况下我们是想让它复位的. 在WPF中也有这个 ...
- [Android] RelativeLayout, LinearLayout,FrameLayout
Android RelativeLayout 属性 // 相对于给定ID控件 android:layout_above 将该控件的底部置于给定ID的控件之上; android:layout_below ...
- Centos 用户登录失败N次后锁定用户禁止登陆
针对linux上的用户,如果用户连续3次登录失败,就锁定该用户,几分钟后该用户再自动解锁 Linux有一个pam_tally2.so的PAM模块,来限定用户的登录失败次数,如果次数达到设置的阈值,则锁 ...
- 跨浏览器的事件对象-------EventUtil 中的方法及用法
什么是EventUti----封装好的事件对象 在JavaScript中,DOM0级.DOM2级与旧版本IE(8-)为对象添加事件的方法不同 为了以跨浏览器的方式处理事件,需要编写一段“通用代码”,即 ...
- C#文本选中及ContextMenuStrip菜单使用
'文本框选中显示'TextBox1.SelectAll()选择所有文本'textBox1.Text.Insert(start,strInsertText)指定位置添加文本1 Private Sub T ...
- javascript 中 filter 方法
filter 方法用来迭代一个数组,并且按给出的条件过滤出符合的元素. filter 方法传入一个回调函数,这个回调函数会携带一个参数,参数为当前迭代的项(我们叫它 val ). 回调函数返回 tru ...
- sql server 行转列(转载)
SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PI ...