synchronized 块
来源:互联网 发布:c语言 string[] 编辑:程序博客网 时间:2024/04/28 18:45
方法一.
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 块
- 【多线程】synchronized同步块
- Synchronized块同步变量
- synchronized代码块调度
- synchronized同步语句块
- synchronized同步代码块
- synchronized同步代码块
- synchronized同步代码块
- synchronized同步语句块
- synchronized方法和synchronized块的区别
- synchronized块和synchronized方法的区别
- java synchronized详解--synchronized代码块
- 使用synchronized块同步方法..
- 使用Synchronized块同步方法
- 使用Synchronized块同步变量
- 有效的使用和设计COM智能指针——条款3:按照功能和实现原理选择合适的智能指针
- 敬业,功夫在诗外
- Android 标题 居中
- shell判断文件
- PowerPivot
- synchronized 块
- struts,hibernate 中日期转换问题
- 归档工作的重要性
- 模拟器启动不起来怎么解决
- Sql Server 2005 服务器性能监视
- 有效的使用和设计COM智能指针——条款4:理解ATL的CComPtr提倡简单,高效
- in 函数
- Silverlight嵌入HTML和Flash和PDF文件
- Android系统移植-按键移植