㈠ java中的sleep和wait的区别
java中的sleep和wait的区别如下:
1、所属的超类不同:sleep属于线程Thread类的方法,而wait属于Object方法
2、用法不同:
①sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。
②调用wait()方法的时候,线程会放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象调用notify()方法后本线程才进入对象锁定池准备
3、举例说明:
wait方法:
private static class Thread1 implements Runnable{
@Override
public void run(){
synchronized (TestD.class) {
System.out.println("enter thread1...");
System.out.println("thread1 is waiting...");
try {
//调用wait()方法,线程会放弃对象锁,进入等待此对象的等待锁定池
TestD.class.wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread1 is going on ....");
System.out.println("thread1 is over!!!");
}
}
}
sleep方法用法:
private static class Thread2 implements Runnable{
@Override
public void run(){
synchronized (TestD.class) {
System.out.println("enter thread2....");
System.out.println("thread2 is sleep....");
//只有针对此对象调用notify()方法后本线程才进入对象锁定池准备获取对象锁进入运行状态。
TestD.class.notify();
//==================
//区别
//如果我们把代码:TestD.class.notify();给注释掉,即TestD.class调用了wait()方法,但是没有调用notify()
//方法,则线程永远处于挂起状态。
try {
//sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,
//但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。
//在调用sleep()方法的过程中,线程不会释放对象锁。
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread2 is going on....");
System.out.println("thread2 is over!!!");
}
}
}
运行结果:
enter thread1...
thread1 is waiting...
enter thread2....
thread2 is sleep....
thread2 is going on....
thread2 is over
thread1 is going on ....
thread1 is over
㈡ Java Thread BLOCKED和WAITING两种状态的区别
BLOCKED状态
线程处于BLOCKED状态的场景。
当前线程在等待一个monitor lock,比如等待执行synchronized代码块或者使用synchronized标记的方法。
在synchronized块中循环调用Object类型的wait方法,如下是样例
synchronized(this)
{
while (flag)
{
obj.wait();
}
// some other code
}
WAITING状态
线程处于WAITING状态的场景。
调用Object对象的wait方法,但没有指定超时值。
调用Thread对象的join方法,但没有指定超时值。
调用LockSupport对象的park方法。
提到WAITING状态,顺便提一下TIMED_WAITING状态的场景。
TIMED_WAITING状态
线程处于TIMED_WAITING状态的场景。
调用Thread.sleep方法。
调用Object对象的wait方法,指定超时值。
调用Thread对象的join方法,指定超时值。
调用LockSupport对象的parkNanos方法。
调用LockSupport对象的parkUntil方法。
㈢ java wait
java wait是怎样的呢?下面就让我们一起来了解一下吧:
wait()方法是属于java中的一个方法,它的作用是能够让当前线程进入等待状态,同时,wait()也会让当前线程释放它所持有的锁。直到其他线程调用此对象的notify()方法或者notifyAll()方法,当前线程被唤醒(也就是进入“就绪状态”)。
说明:
notify()和notifyAll()方法的作用,则是用于唤醒当前对象上的等待线程;notify()方法是唤醒单个线程,而notifyAll()是唤醒所有的线程。
参考范例:
packagecom.citi.test.mutiplethread.demo0503;importjava.util.Date;publicclassWaitTest{publicstaticvoidmain(String[]args){ThreadAt1=newThreadA(t1);System.out.println(t1:+t1);synchronized(t1){try{//启动线程System.out.println(Thread.currentThread().getName()+startt1);t1.start();//主线程等待t1通过notify唤醒。System.out.println(Thread.currentThread().getName()+wait()+newDate());t1.wait();//不是使t1线程等待,而是当前执行wait的线程等待System.out.println(Thread.currentThread().getName()+continue+newDate());}catch(Exceptione){e.printStackTrace();}}}}classThreadAextendsThread{publicThreadA(Stringname){super(name);}@Overridepublicvoidrun(){synchronized(this){System.out.println(this:+this);try{Thread.sleep(2000);//使当前线程阻塞1秒}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}System.out.println(Thread.currentThread().getName()+callnotify());this.notify();}}}