1. 程式人生 > >Android中物件鎖的wait()和notify()

Android中物件鎖的wait()和notify()

基本概念:物件鎖synchronized(object){….}用法
在以上的程式碼塊中只能由一個執行緒執行!!!
wait()、notify()是用在這個程式碼塊當中的。wait()可以使當前執行緒A馬上失去物件鎖並且沉睡,直到物件呼叫notify()喚醒該執行緒。此時持有物件鎖的執行緒B會先行執行完畢,然後再將物件鎖交給執行緒A繼續執行。
例子說明:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this
.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }

首先建立一個供執行緒競爭的Person類,含有構造方法和get、set方法。

final Person person = new Person("王龍",23);
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (person){
                    try {
                        for (int i = 0 ; i<10 ; i++) {
                            Thread.sleep(1000
); Log.e(person.getName(),"觀自在菩薩,行深般若波若蜜多時"+i); if (i==5){ person.wait(); } } } catch (InterruptedException e) { e.printStackTrace(); } } } },"Thread1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { synchronized (person){ try { for ( int i = 0 ; i < 10 ; i++ ){ Thread.sleep(1000); Log.e(person.getName(),"照見五蘊皆空,度一切苦厄"+i); if (i==5){ person.notify(); } } } catch (InterruptedException e) { e.printStackTrace(); } } } },"Thread2"); t1.start(); t2.start();

執行緒A和執行緒B是併發執行的。執行緒A每隔1s列印一條日誌。當迴圈到第5次的是否wait()放棄物件鎖並沉睡。此時執行緒B獲得物件鎖開始執行。執行到第5次迴圈。notify()喚醒執行緒A。

執行結果:

01-13 15:45:18.335 15435-15906/com.huaxinzhi.usingthreadpool E/王龍: 觀自在菩薩,行深般若波若蜜多時0
01-13 15:45:19.335 15435-15906/com.huaxinzhi.usingthreadpool E/王龍: 觀自在菩薩,行深般若波若蜜多時1
01-13 15:45:20.335 15435-15906/com.huaxinzhi.usingthreadpool E/王龍: 觀自在菩薩,行深般若波若蜜多時2
01-13 15:45:21.335 15435-15906/com.huaxinzhi.usingthreadpool E/王龍: 觀自在菩薩,行深般若波若蜜多時3
01-13 15:45:22.335 15435-15906/com.huaxinzhi.usingthreadpool E/王龍: 觀自在菩薩,行深般若波若蜜多時4
01-13 15:45:23.335 15435-15906/com.huaxinzhi.usingthreadpool E/王龍: 觀自在菩薩,行深般若波若蜜多時5
01-13 15:45:24.335 15435-15907/com.huaxinzhi.usingthreadpool E/王龍: 照見五蘊皆空,度一切苦厄0
01-13 15:45:25.335 15435-15907/com.huaxinzhi.usingthreadpool E/王龍: 照見五蘊皆空,度一切苦厄1
01-13 15:45:26.335 15435-15907/com.huaxinzhi.usingthreadpool E/王龍: 照見五蘊皆空,度一切苦厄2
01-13 15:45:27.335 15435-15907/com.huaxinzhi.usingthreadpool E/王龍: 照見五蘊皆空,度一切苦厄3
01-13 15:45:28.335 15435-15907/com.huaxinzhi.usingthreadpool E/王龍: 照見五蘊皆空,度一切苦厄4
01-13 15:45:29.335 15435-15907/com.huaxinzhi.usingthreadpool E/王龍: 照見五蘊皆空,度一切苦厄5
01-13 15:45:30.335 15435-15907/com.huaxinzhi.usingthreadpool E/王龍: 照見五蘊皆空,度一切苦厄6
01-13 15:45:31.335 15435-15907/com.huaxinzhi.usingthreadpool E/王龍: 照見五蘊皆空,度一切苦厄7
01-13 15:45:32.335 15435-15907/com.huaxinzhi.usingthreadpool E/王龍: 照見五蘊皆空,度一切苦厄8
01-13 15:45:33.335 15435-15907/com.huaxinzhi.usingthreadpool E/王龍: 照見五蘊皆空,度一切苦厄9
01-13 15:45:34.335 15435-15906/com.huaxinzhi.usingthreadpool E/王龍: 觀自在菩薩,行深般若波若蜜多時6
01-13 15:45:35.345 15435-15906/com.huaxinzhi.usingthreadpool E/王龍: 觀自在菩薩,行深般若波若蜜多時7
01-13 15:45:36.345 15435-15906/com.huaxinzhi.usingthreadpool E/王龍: 觀自在菩薩,行深般若波若蜜多時8
01-13 15:45:37.345 15435-15906/com.huaxinzhi.usingthreadpool E/王龍: 觀自在菩薩,行深般若波若蜜多時9

以上!