開心一笑
笑一笑十年少
受傷的烏龜
烏龜受傷懒震。讓蝸牛去買藥。過了2個小時荞胡。蝸牛還沒回來。烏龜急了罵道:他媽的再不回來老子就死了了嚎!這時門外傳來了蝸牛的聲音:你他媽再說老子不去了泪漂!
提出問題
解決問題
前面已經自我介紹了,今天是最后一次歪泳,我是超級奶爸萝勤,英文名:Object......這篇介紹完后,我就不再介紹我的夹囚,把機會留給我的子子孫孫們......
wait()
導致線程進入等待狀態(tài)纵刘,直到它被其他線程通過notify()或者notifyAll喚醒。我有幾個哥們:wait(long timeout) 荸哟,wait(long timeout, int nanos) 假哎,timeout時間單位為毫秒,nano是毫微秒......
錯誤寫法
/**
* Created by 阿毅 on 2016/2/26.
*/
public class TestObejct {
public synchronized void test() throws InterruptedException{
Thread thread = Thread.currentThread();//獲得當前線程
System.out.println("Thread ID:" + thread.getId() + "Thread Name:" + thread.getName() );
}
}
/**
* Created by 阿毅 on 2016/2/26.
*/
public class ObjectWaitTest {
public static void main(String[] args) {
TestObejct testObejct = new TestObejct();
try {
testObejct.test();
testObejct.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- 執(zhí)行結果回報錯
- Object.wait()和Object.notify()和Object.notifyall()必須寫在synchronized方法內部或者synchronized塊內部
- synchronized就是針對內存區(qū)塊申請內存鎖(會在之后給出更多鎖的介紹)
正確寫法:
/**
* Created by 阿毅 on 2016/2/26.
*/
public class TestObejct {
public synchronized void test() throws InterruptedException{
Thread thread = Thread.currentThread();//獲得當前線程
System.out.println("Thread ID:" + thread.getId() + "Thread Name:" + thread.getName() );
//wait();//一直傻傻的等鞍历,直到別人叫醒
wait(1000);//等1s
//wait(1000,100);//等一秒多啦
}
}
/**
* Created by 阿毅 on 2016/2/26.
*/
public class ObjectWaitTest {
public static void main(String[] args) {
TestObejct testObejct = new TestObejct();
try {
testObejct.test();
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread thread = Thread.currentThread();
System.out.println("Thread ID:" + thread.getId()
+ "Thread Name" + thread.getName());
}
}
//結果 打印兩次相隔1s
Thread ID:1Thread Name:main
Thread ID:1Thread Namemain
notify()
喚醒在此對象監(jiān)視器上等待的單個線程
> 以下代碼引用:http://www.cnblogs.com/dolphin0520/p/3920385.html
/**
* Created by 阿毅 on 2016/2/26.
*/
public class ObjectNotifyTest {
public static Object object = new Object();
public static void main(String[] args) {
Thread1 thread1 = new Thread1();
Thread2 thread2 = new Thread2();
thread1.start();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.start();
}
static class Thread1 extends Thread{
@Override
public void run() {
synchronized (object) {
try {
object.wait();
} catch (InterruptedException e) {
}
System.out.println("線程"+Thread.currentThread().getName()+"獲取到了鎖");
}
}
}
static class Thread2 extends Thread{
@Override
public void run() {
synchronized (object) {
object.notify();
System.out.println("線程"+Thread.currentThread().getName()+"調用了object.notify()");
}
System.out.println("線程"+Thread.currentThread().getName()+"釋放了鎖");
}
}
}
//運行結果
線程Thread-1調用了object.notify()
線程Thread-1釋放了鎖
線程Thread-0獲取到了鎖
- 上面代碼都是對object進行加鎖的舵抹。
- 從上面運行結果可以知道,一個線程被喚醒不代表立即獲取了對象的monitor劣砍,
只有等調用完notify()或者notifyAll()并退出synchronized塊惧蛹,釋放對象鎖后,其余線程才可獲得鎖執(zhí)行。
notifyAll()
喚醒所有在此對象監(jiān)視器上等待的單個線程
優(yōu)秀文章
讀書感悟
來自《泰坦尼克號》
- you jump 香嗓,I jump......
- 要讓每一天有所值
- 別這樣迅腔,堅持下去,你明白嗎靠娱?