JAvaスレッドsleepとwaitの違い
3751 ワード
JAvaスレッドThreadクラスのsleep静的メソッドは、現在のスレッドをスリープさせることができます(スレッドがオブジェクトのロックを持っている場合、sleepはロックを解放しません.スレッドにオブジェクトのロックがない場合、sleepもオブジェクトをロックしません).
一方、waitメソッドは、スレッドをブロックしてしばらく眠らせたり、他のスレッドの起動を待ったりすることもできるが、waitメソッドを呼び出して睡眠に入ると、スレッドがオブジェクトロックを持っている場合、スレッドが睡眠に入ると、スレッドが持っているオブジェクトロックが解放される.
参考例:
実行結果:
main start running time 0
Thread-0 start running time 0
main end running time 3
Threadに変更します.sleep
実行結果:
main start running time 0
main end running time 3
Thread-0 start running time 3
しかしfun 1を静的方法に置き換えると、
実行結果:
main start running time 0
Thread-0 start running time 0
main end running time 3
これは、方法が静的である場合、スレッドがTaskクラスに対応するClassクラスのオブジェクトのロックをロックし、Thread.sleepロックはTaskクラスのオブジェクトであり,2つの異なるオブジェクトロックであるため,以上の結果が得られる.
一方、waitメソッドは、スレッドをブロックしてしばらく眠らせたり、他のスレッドの起動を待ったりすることもできるが、waitメソッドを呼び出して睡眠に入ると、スレッドがオブジェクトロックを持っている場合、スレッドが睡眠に入ると、スレッドが持っているオブジェクトロックが解放される.
参考例:
/**
* sleep wait
*/
class Task implements Runnable{
private int time = 0;
public void run(){
fun1();
}
public synchronized void fun1(){
System.out.println(Thread.currentThread().getName()+ " start running time "+ time);
}
public synchronized void fun2(){
try{
System.out.println(Thread.currentThread().getName()+ " start running time "+ time);
// Thread.sleep(3000);
wait(3000);
time += 3;
System.out.println(Thread.currentThread().getName()+ " end running time "+ time);
}catch(Exception e){
e.printStackTrace();
}
}
}
public class ThreadTest{
public static void main(String[] args){
Task task = new Task();
Thread thread = new Thread(task);
thread.start();
task.fun2();
}
}
実行結果:
main start running time 0
Thread-0 start running time 0
main end running time 3
Threadに変更します.sleep
/**
* sleep wait
*/
class Task implements Runnable{
private int time = 0;
public void run(){
fun1();
}
public synchronized void fun1(){
System.out.println(Thread.currentThread().getName()+ " start running time "+ time);
}
public synchronized void fun2(){
try{
System.out.println(Thread.currentThread().getName()+ " start running time "+ time);
// Thread.sleep(3000);
wait(3000);
time += 3;
System.out.println(Thread.currentThread().getName()+ " end running time "+ time);
}catch(Exception e){
e.printStackTrace();
}
}
}
public class ThreadTest{
public static void main(String[] args){
Task task = new Task();
Thread thread = new Thread(task);
thread.start();
task.fun2();
}
}
実行結果:
main start running time 0
main end running time 3
Thread-0 start running time 3
しかしfun 1を静的方法に置き換えると、
/**
* sleep wait
*/
class Task implements Runnable{
private static int time = 0;
public void run(){
fun1();
}
public static synchronized void fun1(){
System.out.println(Thread.currentThread().getName()+ " start running time "+ time);
}
public synchronized void fun2(){
try{
System.out.println(Thread.currentThread().getName()+ " start running time "+ time);
Thread.sleep(3000);
// wait(3000);
time += 3;
System.out.println(Thread.currentThread().getName()+ " end running time "+ time);
}catch(Exception e){
e.printStackTrace();
}
}
}
public class ThreadTest{
public static void main(String[] args){
Task task = new Task();
Thread thread = new Thread(task);
thread.start();
task.fun2();
}
}
実行結果:
main start running time 0
Thread-0 start running time 0
main end running time 3
これは、方法が静的である場合、スレッドがTaskクラスに対応するClassクラスのオブジェクトのロックをロックし、Thread.sleepロックはTaskクラスのオブジェクトであり,2つの異なるオブジェクトロックであるため,以上の結果が得られる.