首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

线程中的synchronized运用

2012-09-24 
线程中的synchronized使用?private static class ListenerHandlerThread implements Runnable {?private L

线程中的synchronized使用

?

private static class ListenerHandlerThread implements Runnable {

?

private Looper looper;

?

private byte[] listenerKey = new byte[0];

?

public ListenerHandlerThread() {

new Thread(this).start();

synchronized (listenerKey) {

while (looper == null) { ?//注意while必须放synronized里面,不然会出错

try {

listenerKey.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

?

public Looper getLooper() {

return looper;

}

?

@Override

public void run() {

Looper.prepare();

looper = Looper.myLooper();

synchronized (listenerKey) {

listenerKey.notifyAll();

}

Looper.loop();

}

?

}

热点排行