线程操作经常用到wait和notify,用起来稍显繁琐,而Android给我们封装好了一个ConditionVariable类,用于线程同步。提供了三个方法block()、open()、close()。
void block()
//阻塞当前线程,直到条件为open
void block(long timeout)
//阻塞当前线程,直到条件为open或超时
void open()
//释放所有阻塞的线程
void close()
//将条件重置为close
ConditionVariable 在创建时还有一种构造方法是 public ConditionVariable (boolean state) ,如果为true,默认时为opened,如果为false则是closed. ,默认public ConditionVariable()为closed.
其实很好理解
private volatile boolean mCondition;
成员内部变量。
//默认构造函数public ConditionVariable()
{mCondition = false;
}
public ConditionVariable(boolean state)
{mCondition = state;
}
open,释放阻塞,即notifyAll一下,此时成员变量变为true。
public void open(){synchronized (this) {boolean old = mCondition;mCondition = true;if (!old) {this.notifyAll();}}
}
close,重置成员变量为false
public void close(){synchronized (this) {mCondition = false;}
}
block,只有在成员变量为false的时候进行wait等待
public void block(){synchronized (this) {while (!mCondition) {try {this.wait();}catch (InterruptedException e) {}}}
}
public boolean block(long timeout){// Object.wait(0) means wait forever, to mimic this, we just// call the other block() method in that case. It simplifies// this code for the common case.if (timeout != 0) {synchronized (this) {long now = System.currentTimeMillis();long end = now + timeout;while (!mCondition && now < end) {try {this.wait(end-now);}catch (InterruptedException e) {}now = System.currentTimeMillis();}return mCondition;}} else {this.block();return true;}
}
简单的延时:
public class MainActivity extends Activity implements OnClickListener {private Button btn_output;private TextView textView;private MyHandler mHandler;private boolean isStart;private String str = "";private ConditionVariable mConditionVariable;private final int REFRESHTEXT = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取按钮btn_output = (Button) findViewById(R.id.btn);btn_output.setOnClickListener(this);textView = (TextView) findViewById(R.id.txt);mHandler = new MyHandler();mConditionVariable = new ConditionVariable();isStart = true;new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubwhile(isStart) {//延时等待3秒mConditionVariable.block(3000);//如果是点击了按钮,则先将条件重置,否则block会失效mConditionVariable.close();//线程唤醒后通知主线程更新TextView的文本mHandler.sendEmptyMessage(REFRESHTEXT);}}}).start();}private class MyHandler extends Handler {public void handleMessage(Message msg) {switch(msg.what) {case REFRESHTEXT:textView.setText(str += 's');break;}}}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubmConditionVariable.open();}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();isStart = false;}
}
尽管现在诞生的高级语言里边有了什么STM,协程,绿程的概念,但写代码总会遇到现实(商业级平台都不会用很新的东西)的多线程的问题。
比如有时候你需要同步的获取在另一个线程执行的代码的结果,在android里这种场景下ConditionVariable就非常好用了。
if (Looper.myLooper() != Looper.getMainLooper()) {final ConditionVariable completed = new ConditionVariable(); // 构造一个条件变量view.runOnUiThread(new Runnable() {@Overridepublic void run() {try {doSomeThingInUiThread(); // 将期望在另外线程做的事post出去} finally { // finally很重要,防止运行时异常远跳转将ConditionVariable忘了opencompleted.open(); // 事情办完了,notify到另外线程}}
});completed.block(TIMEOUT_WAIT_UI); // 等着另外线程做的事完成,同时支持设置超时
} else {doSomeThingInUiThread();
}
当然了以上是很简单的一个场景,使用ConditionVariable非常方便且够用。对于复杂的多线程之间的协同还是使用标准的条件变量结合lock + while 循环检查。
在一些新语言中有非常丰富的并发编程原语(future, delay, promise),特别是协程让我们用代码自主的确定代码流之间的协作关系而不是被动的作为OS调度器的奴隶,来支撑一些并行需求。
参考链接:https://www.jianshu.com/p/c03aa7bf3ebf
https://www.cnblogs.com/yydcdut/p/4273526.html
https://developer.aliyun.com/article/24161