概要
本章卷谈,會(huì)對(duì)Thread中sleep()方法進(jìn)行介紹锭弊。涉及到的內(nèi)容包括:
- sleep()介紹
- sleep()示例
- sleep() 與 wait()的比較
轉(zhuǎn)載請(qǐng)注明出處:http://www.cnblogs.com/skywang12345/p/3479256.html
1. sleep()介紹
sleep() 定義在Thread.Java中反粥。sleep() 的作用是讓當(dāng)前線程休眠应闯,即當(dāng)前線程會(huì)從“運(yùn)行狀態(tài)”進(jìn)入到“休眠(阻塞)狀態(tài)”茸习。sleep()會(huì)指定休眠時(shí)間柔逼,線程休眠的時(shí)間會(huì)大于/等于該休眠時(shí)間;在線程重新被喚醒時(shí)享钞,它會(huì)由“阻塞狀態(tài)”變成“就緒狀態(tài)”揍诽,從而等待cpu的調(diào)度執(zhí)行。
2. sleep()示例
下面通過(guò)一個(gè)簡(jiǎn)單示例演示sleep()的用法栗竖。
1 // SleepTest.java的源碼
2 class ThreadA extends Thread{
3 public ThreadA(String name){
4 super(name);
5 }
6 public synchronized void run() {
7 try {
8 for(int i=0; i <10; i++){
9 System.out.printf("%s: %d\n", this.getName(), i);
10 // i能被4整除時(shí)暑脆,休眠100毫秒
11 if (i%4 == 0)
12 Thread.sleep(100);
13 }
14 } catch (InterruptedException e) {
15 e.printStackTrace();
16 }
17 }
18 }
19
20 public class SleepTest{
21 public static void main(String[] args){
22 ThreadA t1 = new ThreadA("t1");
23 t1.start();
24 }
25 }
運(yùn)行結(jié)果:
t1: 0
t1: 1
t1: 2
t1: 3
t1: 4
t1: 5
t1: 6
t1: 7
t1: 8
t1: 9
結(jié)果說(shuō)明:
程序比較簡(jiǎn)單,在主線程main中啟動(dòng)線程t1狐肢。t1啟動(dòng)之后添吗,當(dāng)t1中的計(jì)算i能被4整除時(shí),t1會(huì)通過(guò)Thread.sleep(100)休眠100毫秒份名。
3. sleep() 與 wait()的比較
我們知道碟联,wait()的作用是讓當(dāng)前線程由“運(yùn)行狀態(tài)”進(jìn)入“等待(阻塞)狀態(tài)”的同時(shí),也會(huì)釋放同步鎖僵腺。而sleep()的作用是也是讓當(dāng)前線程由“運(yùn)行狀態(tài)”進(jìn)入到“休眠(阻塞)狀態(tài)”鲤孵。但是,wait()會(huì)釋放對(duì)象的同步鎖辰如,而sleep()則不會(huì)釋放鎖普监。下面通過(guò)示例演示sleep()是不會(huì)釋放鎖的。
1 // SleepLockTest.java的源碼
2 public class SleepLockTest{
3
4 private static Object obj = new Object();
5
6 public static void main(String[] args){
7 ThreadA t1 = new ThreadA("t1");
8 ThreadA t2 = new ThreadA("t2");
9 t1.start();
10 t2.start();
11 }
12
13 static class ThreadA extends Thread{
14 public ThreadA(String name){
15 super(name);
16 }
17 public void run(){
18 // 獲取obj對(duì)象的同步鎖
19 synchronized (obj) {
20 try {
21 for(int i=0; i <10; i++){
22 System.out.printf("%s: %d\n", this.getName(), i);
23 // i能被4整除時(shí)琉兜,休眠100毫秒
24 if (i%4 == 0)
25 Thread.sleep(100);
26 }
27 } catch (InterruptedException e) {
28 e.printStackTrace();
29 }
30 }
31 }
32 }
33 }
運(yùn)行結(jié)果:
t1: 0
t1: 1
t1: 2
t1: 3
t1: 4
t1: 5
t1: 6
t1: 7
t1: 8
t1: 9
t2: 0
t2: 1
t2: 2
t2: 3
t2: 4
t2: 5
t2: 6
t2: 7
t2: 8
t2: 9
結(jié)果說(shuō)明:
主線程main中啟動(dòng)了兩個(gè)線程t1和t2鹰椒。t1和t2在run()會(huì)引用同一個(gè)對(duì)象的同步鎖,即synchronized(obj)呕童。在t1運(yùn)行過(guò)程中,雖然它會(huì)調(diào)用Thread.sleep(100)淆珊;但是夺饲,t2是不會(huì)獲取cpu執(zhí)行權(quán)的。因?yàn)槭┓瑃1并沒(méi)有釋放“obj所持有的同步鎖”往声!
注意,若我們注釋掉synchronized (obj)后再次執(zhí)行該程序戳吝,t1和t2是可以相互切換的浩销。下面是注釋調(diào)synchronized(obj) 之后的源碼:
1 // SleepLockTest.java的源碼(注釋掉synchronized(obj))
2 public class SleepLockTest{
3
4 private static Object obj = new Object();
5
6 public static void main(String[] args){
7 ThreadA t1 = new ThreadA("t1");
8 ThreadA t2 = new ThreadA("t2");
9 t1.start();
10 t2.start();
11 }
12
13 static class ThreadA extends Thread{
14 public ThreadA(String name){
15 super(name);
16 }
17 public void run(){
18 // 獲取obj對(duì)象的同步鎖
19 // synchronized (obj) {
20 try {
21 for(int i=0; i <10; i++){
22 System.out.printf("%s: %d\n", this.getName(), i);
23 // i能被4整除時(shí),休眠100毫秒
24 if (i%4 == 0)
25 Thread.sleep(100);
26 }
27 } catch (InterruptedException e) {
28 e.printStackTrace();
29 }
30 // }
31 }
32 }
33 }