线程 同步 ConditionVariable-------特别使用在mediaplayer和camera多次调用中.

转自: http://blog.csdn.net/shaojie519/article/details/6684326

ConditionVariable类位于android.os.ConditionVariable,它可以帮助Android线程同步。在SDK上的介绍ConditionVariable不同于标准Java位于java.lang.Object wait() 和 notify() ,这个类可以等待自己,这就意味着 open(), close() 和 block() 可能会假死 ,如果使用ConditionVariable类的open()在调用 block() 之前, block() 将不会阻塞,相反将会返回立即。


该类一共有4个方法

boolean  block(long timeout)
阻止当前线程知道条件是open,或直到超时,这里参数long timeout为超时设置,Android123提示大家如果你们从事过Win32开发,这个方法类似DWORD WaitForSingleObject(HANDLE hHandle,DWORD dwMilliseconds); 函数。

void  block()
阻止当前线程知道条件 open ,是上面的无超时等待重载版本。

void  close()
重置条件为 close状态。

void  open()
Open条件,释放所有线程的阻塞.

ConditionVariable在创建时还有一种构造方法是 public ConditionVariable (boolean state) ,如果为true,默认时为opened,如果为false则是closed. ,默认public ConditionVariable()为close()

给出api中一个demo:

/* 
 * Copyright (C) 2007 The Android Open Source Project 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */  
  
package com.example.android.apis.app;  
  
// Need the following import to get access to the app resources, since this  
// class is in a sub-package.  
import com.example.android.apis.R;  
  
import android.app.Notification;  
import android.app.NotificationManager;  
import android.app.PendingIntent;  
import android.app.Service;  
import android.content.Intent;  
import android.os.Binder;  
import android.os.ConditionVariable;  
import android.os.IBinder;  
import android.os.Parcel;  
import android.os.RemoteException;  
import android.widget.RemoteViews;  
  
/** 
 * This is an example of service that will update its status bar balloon  
 * every 5 seconds for a minute. 
 *  
 */  
public class NotifyingService extends Service {  
      
    // Use a layout id for a unique identifier  
    private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;  
  
    // variable which controls the notification thread   
    private ConditionVariable mCondition;//它可以帮助Android线程同步  
   
    @Override  
    public void onCreate() {  
        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  
        // Start up the thread running the service.  Note that we create a  
        // separate thread because the service normally runs in the process's  
        // main thread, which we don't want to block.  
        Thread notifyingThread = new Thread(null, mTask, "NotifyingService");  
        mCondition = new ConditionVariable(false);  
        notifyingThread.start();  
    }  
  
    @Override  
    public void onDestroy() {  
        // Cancel the persistent notification.  
        mNM.cancel(MOOD_NOTIFICATIONS);  
        // Stop the thread from generating further notifications  
        mCondition.open();   
    }  
  
    private Runnable mTask = new Runnable() {  
        public void run() {  
            for (int i = 0; i < 4; ++i) {  
                showNotification(R.drawable.stat_happy,  
                        R.string.status_bar_notifications_happy_message);  
                if (mCondition.block(5 * 1000))   
                    break;  
                showNotification(R.drawable.stat_neutral,  
                        R.string.status_bar_notifications_ok_message);  
                if (mCondition.block(5 * 1000))   
                    break;  
                showNotification(R.drawable.stat_sad,  
                        R.string.status_bar_notifications_sad_message);  
                if (mCondition.block(5 * 1000))   
                    break;  
            }  
            // Done with our work...  stop the service!  
            NotifyingService.this.stopSelf();  
        }  
    };  
  
    @Override  
    public IBinder onBind(Intent intent) {  
        return mBinder;  
    }  
      
    private void showNotification(int moodId, int textId) {  
        // In this sample, we'll use the same text for the ticker and the expanded notification  
        CharSequence text = getText(textId);  
  
        // Set the icon, scrolling text and timestamp.  
        // Note that in this example, we pass null for tickerText.  We update the icon enough that  
        // it is distracting to show the ticker text every time it changes.  We strongly suggest  
        // that you do this as well.  (Think of of the "New hardware found" or "Network connection  
        // changed" messages that always pop up)  
        Notification notification = new Notification(moodId, null, System.currentTimeMillis());  
  
        // The PendingIntent to launch our activity if the user selects this notification  
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
                new Intent(this, NotifyingController.class), 0);  
  
        // Set the info for the views that show in the notification panel.  
        notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title),  
                       text, contentIntent);  
  
        // Send the notification.  
        // We use a layout id because it is a unique number.  We use it later to cancel.  
        mNM.notify(MOOD_NOTIFICATIONS, notification);  
    }  
  
    // This is the object that receives interactions from clients.  See  
    // RemoteService for a more complete example.  
    private final IBinder mBinder = new Binder() {  
        @Override  
        protected boolean onTransact(int code, Parcel data, Parcel reply,  
                int flags) throws RemoteException {  
            return super.onTransact(code, data, reply, flags);  
        }  
    };  
  
    private NotificationManager mNM;  
}  

另一篇文章---------------

转自:http://blog.csdn.net/pocoyoshamoo/article/details/9156089

在Android开发中,如果需要线程同步,可以使用Java系统库的wait()和notify()...但是这些代码的编写比较麻烦。Android提供了一个ConditionVariable类方便线程同步操作。

  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.


  自己简单写了一个Demo,效果有点牵强...Demo的效果是,TextView每过3秒,就会输出一个字符s。如果点击按钮可以马上输出。

[java]  view plain copy
  1. public class MainActivity extends Activity implements OnClickListener {  
  2.     private Button btn_output;  
  3.     private TextView textView;  
  4.     private MyHandler mHandler;  
  5.     private boolean isStart;  
  6.     private String str = "";  
  7.     private ConditionVariable mConditionVariable;  
  8.     private final int REFRESHTEXT = 1;  
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.         btn_output = (Button) findViewById(R.id.btn);  
  14.         btn_output.setOnClickListener(this);  
  15.         textView = (TextView) findViewById(R.id.txt);  
  16.         mHandler = new MyHandler();  
  17.         mConditionVariable = new ConditionVariable();  
  18.         isStart = true;  
  19.         new Thread(new Runnable() {  
  20.             @Override  
  21.             public void run() {  
  22.                 // TODO Auto-generated method stub  
  23.                 while(isStart) {  
  24.                     //延时等待3秒  
  25.                     mConditionVariable.block(3000);  
  26.                     //如果是点击了按钮,则先将条件重置,否则block会失效  
  27.                     mConditionVariable.close();  
  28.                     //线程唤醒后通知主线程更新TextView的文本  
  29.                     mHandler.sendEmptyMessage(REFRESHTEXT);  
  30.                 }  
  31.             }  
  32.         }).start();  
  33.     }  
  34.   
  35.     private class MyHandler extends Handler {  
  36.         public void handleMessage(Message msg) {  
  37.             switch(msg.what) {  
  38.             case REFRESHTEXT:  
  39.                 textView.setText(str += 's');  
  40.                 break;  
  41.             }  
  42.         }  
  43.     }  
  44.       
  45.     @Override  
  46.     public void onClick(View v) {  
  47.         // TODO Auto-generated method stub  
  48.         mConditionVariable.open();  
  49.     }  
  50.   
  51.     @Override  
  52.     protected void onDestroy() {  
  53.         // TODO Auto-generated method stub  
  54.         super.onDestroy();  
  55.         isStart = false;  
  56.     }  
  57. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值