synchronized 塊
方法一.
private class Worker implements Runnable {
private Object mLock = new Object();
private int spantime = 100;
private boolean bResult = false;
public Worker(int spantime) {
Log.v(TAG, " TestThread constructor");
this.spantime = spantime;
new Thread(this).start();
}
@Override
public void run() {
Log.v(TAG, " TestThread run");
synchronized (mLock) {
try {
Thread.sleep(spantime);
bResult = true;
Log.v(TAG, "current thread = " + Thread.currentThread().getName() + " worker thread .... count = " + count++);
} catch (InterruptedException e) {
e.printStackTrace();
}
mLock.notifyAll();
}
}
boolean getResult() {
synchronized (mLock) {
try {
mLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return bResult;
}
}
方法二
public void onClick(View v) {
AThread thr = new AThread();
thr.start();
synchronized(thr){
try {
//Thread.sleep(1000);
thr.wait();
Log.v(TAG, " sleep 1000 --> 2");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized(thr){
thr.mReady = true;
Log.v(TAG, " set mReady = true ---> 3");
thr.notifyAll();
}
}
class AThread extends Thread{
public boolean mReady = false;
public AThread(){
super("Athread");
}
public void run(){
Looper.prepare();
synchronized (this){
try {
Thread.sleep(1000);
Log.v(TAG, " Thread notifyall -->1");
this.notifyAll();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized(this){
while (!mReady){
try {
wait();
Log.v(TAG, " Thread wait ----> 4");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Looper.loop();
}
}
相關文章
- synchronized 程式碼塊怎麼用synchronized
- synchronized函式和synchronized程式碼塊有什麼區別?synchronized函式
- 146.synchronized同步方法與塊synchronized
- Java同步塊(synchronized block)使用詳解JavasynchronizedBloC
- synchronized鎖機制 之 程式碼塊鎖synchronized
- Java多執行緒5:synchronized鎖方法塊Java執行緒synchronized
- synchronizedsynchronized
- synchronized探究synchronized
- synchronized 原理synchronized
- Synchronized bnsynchronized
- java synchronizedJavasynchronized
- synchronized原理synchronized
- 一張圖講解物件鎖和關鍵字synchronized修飾方法(程式碼塊)物件synchronized
- 【原創】Java多執行緒初學者指南(12):使用Synchronized塊同步變數Java執行緒synchronized變數
- synchronized學習synchronized
- Synchronized詳解synchronized
- synchronized獵奇synchronized
- Synchronized同步鎖synchronized
- Synchronized 精講synchronized
- 關於synchronizedsynchronized
- 詳解synchronizedsynchronized
- synchronized雜談synchronized
- synchronized 鎖的原理synchronized
- Synchronized 實現原理synchronized
- synchronized實現原理synchronized
- synchronized的使用(一)synchronized
- 徹底理解synchronizedsynchronized
- synchronized底層原理synchronized
- Synchronized的那些事synchronized
- 深入學習synchronizedsynchronized
- 深入淺出 synchronizedsynchronized
- Synchronized關鍵字synchronized
- synchronized的意義synchronized
- java 同步鎖(synchronized)Javasynchronized
- java中的synchronizedJavasynchronized
- synchronized 關鍵字synchronized
- 併發程式設計之synchronized(二)------jvm對synchronized的優化程式設計synchronizedJVM優化
- 從此不怕Synchronized鎖synchronized