ConditionVariable在Android应用开发中的用法
来源:互联网 发布:杭州淘宝服装摄影公司 编辑:程序博客网 时间:2024/05/22 15:26
首先来看下ConditionVariable类的定义:
C:\Program Files\Android\android-sdk\platforms\android-21\android.jar
package android.os;
public class ConditionVariable
{
private volatile boolean mCondition;
/**
* Create the ConditionVariable in the default closed state.
*/
public ConditionVariable()
{
mCondition = false;
}
/**
* Create the ConditionVariable with the given state.
* Pass true for opened and false for closed.
*/
public ConditionVariable(boolean state)
{
mCondition = state;
}
/**
* Open the condition, and release all threads that are blocked.
* Any threads that later approach block() will not block unless close()
* is called.
*/
public void open()
{
synchronized (this) {
boolean old = mCondition;
mCondition = true;
if (!old) {
this.notifyAll();
}
}
}
/**
* Reset the condition to the closed state.
* Any threads that call block() will block until someone calls open.
*/
public void close()
{
synchronized (this) {
mCondition = false;
}
}
/**
* Block the current thread until the condition is opened.
* If the condition is already opened, return immediately.
*/
public void block()
{
synchronized (this) {
while (!mCondition) {
try {
this.wait();
}
catch (InterruptedException e) {
}
}
}
}
/**
* Block the current thread until the condition is opened or until
* timeout milliseconds have passed.
* @param timeout the minimum time to wait in milliseconds.
*
* @return true if the condition was opened, false if the call returns
* because of the timeout.
*/
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;
}
}
}
这里详述了ConditionVariable类的实现过程及其内部函数。该类内部定义了一个volatile 类型的 mCondition 变量,通过控制这个变量的值,来实现对wait()函数的调用,达到控制线程是否阻塞的目的。
这里写了一个简单的实例来模拟整个控制过程
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;
@Override
protected 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() {
@Override
public void run() {
// TODO Auto-generated method stub
while(isStart) {
//延时等待3秒
mConditionVariable.block(3000);
//如果是点击了按钮,则先将条件重置,否则block会失效
mConditionVariable.close();
//线程唤醒后通知主线程更新TextView的文本
mHandler.sendEmptyMessage(REFRESHTEXT);
}
}
}).start();
}
private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case REFRESHTEXT:
textView.setTextSize(20.0f);
textView.setText(str += 's');
break;
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mConditionVariable.open();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
isStart = false;
}
}
运行该实例,你会发现按钮旁边的字符串会由hello world变为S,并且S会一直增加,每隔3秒增加一个S;
如果你有手动点击按钮的话,就会直接增加一个S,而不用等待3秒;
这个案例是从别处借鉴过来的,思路不错,就实验了一把,并且效果也不错。
但这个代码有一处不严谨的地方就是在退出该Activity时,并未将mConditionVariable对象重置,建议在onDestroy()函数中增加mConditionVariable.close()调用会显得更为严谨。
- ConditionVariable在Android应用开发中的用法
- Android ConditionVariable的用法
- Android 线程同步 ConditionVariable的用法
- android ConditionVariable
- Android ConditionVariable
- ConditionVariable的用法
- ConditionVariable的用法
- ConditionVariable的用法
- ConditionVariable的用法
- ConditionVariable的用法
- ConditionVariable的用法
- ConditionVariable的用法
- ConditionVariable的用法 .
- Conditionvariable的用法
- 退避算法在android开发中的应用
- BaseActivity在Android开发中的应用
- MVP模式在Android开发中的应用
- MVP模式在Android开发中的应用
- 灾后重建题解
- 【codevs1906】最长递增子序列问题 最大流
- 容器的综合应用--文本查询程序
- 第39级台阶
- MySQL索引操作命令详解
- ConditionVariable在Android应用开发中的用法
- uva10827 - Maximum sum on a torus(最大子矩阵和变形)
- 练习-ubuntu装机笔记
- centos安装nfs服务1
- elasticsearch RESTful搜索引擎-安装
- DEV--chartcontrol绘制统计图
- Android 热修复使用Gradle Plugin1.5改造Nuwa插件
- 关于新浪SaeApp--个人使用感受。
- 古堡算式