当前位置: 首页 > news >正文

江苏省交通建设厅门户网站卡片式设计的网站

江苏省交通建设厅门户网站,卡片式设计的网站,wordpress怎么重装,代写网站一、学习ConditionVariable之前的复习 如果你不懂wait()、notify()怎么使用#xff0c;最好先复习下我之前的这篇博客#xff0c;怎么使用wait()、notify()实现生产者和消费者的关系 java之wait()、notify()实现非阻塞的生产者和消费者 二、看下ConditionVariable源代码实现…一、学习ConditionVariable之前的复习 如果你不懂wait()、notify()怎么使用最好先复习下我之前的这篇博客怎么使用wait()、notify()实现生产者和消费者的关系 java之wait()、notify()实现非阻塞的生产者和消费者 二、看下ConditionVariable源代码实现 package android.os;/*** Class that implements the condition variable locking paradigm.** p* This differs from the built-in java.lang.Object wait() and notify()* in that this class contains the condition to wait on itself. That means* open(), close() and block() are sticky. If open() is called before block(),* block() will not block, and instead return immediately.** p* This class uses itself as the object to wait on, so if you wait()* or notify() on a ConditionVariable, the results are undefined.*/ 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.* * p* Pass true for opened and false for closed.*/public ConditionVariable(boolean state){mCondition state;}/*** Open the condition, and release all threads that are blocked.** p* 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.** p* 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.** p* 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.** p* If the condition is already opened, return immediately.** param timeout the maximum 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;}} }三、我们分析怎么使用 比如有多个线程需要执行同样的代码的时候我们一般希望当一个线程执行到这里之后后面的线程在后面排队然后等之前的线程执行完了再让这个线程执行我们一般用synchronized实现但是这里我们也可以用ConditionVariable实现从源码可以看到我们初始化可以传递一个boolean类型的参数进去我们可以传递true进去 public ConditionVariable(boolean state){mCondition state;} 然后你看下ConditionVariable类里面这个方法 public void block(){synchronized (this) {while (!mCondition) {try {this.wait();}catch (InterruptedException e) {}}}} 如果第一次初始化的时候mCondition是true,那么第一次调用这里就不会走到wait函数然后我们应该需要一个开关让mCondition变成false,让第二个线程进来的时候我们应该让线程执行wait()方法阻塞在这里这不看下ConditionVariable类里面这个函数 public void close(){synchronized (this) {mCondition false;}} 这不恰好是我们需要的我们可以马上调用这个函数close(),然后让程序执行我们想执行的代码最后要记得调用open方法如下 public void open(){synchronized (this) {boolean old mCondition;mCondition true;if (!old) {this.notifyAll();}}} 因为这里调用了notifyAll方法把之前需要等待的线程呼唤醒 所以我们使用可以这样使用 1、初始化 ConditionVariable mLock new ConditionVariable(true); 2、同步的地方这样使用 mLock.block();mLock.close();/**你的代码**/mLock.open(); 四、测试代码分析 我先给出一个原始Demo public class MainActivity extends ActionBarActivity {public static final String TAG ConditionVariable_Test;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);for (int i 0; i 10; i) {new Mythread( i).start();}}public int num 5;class Mythread extends Thread {String name;public Mythread(String name) {this.name name;}Overridepublic void run() {while (true) {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}num--;if (num 0)Log.i(TAG, thread name is: name num is: num);elsebreak;}}} } 运行的结果是这样的 ConditionVariable_Test I thread name is:0 num is:4I thread name is:1 num is:3I thread name is:2 num is:2I thread name is:3 num is:1I thread name is:4 num is:0很明显不是我们想要的结果因为我想一个线程进来了需要等到执行完了才让另外一个线程才能进来 我们用ConditionVariable来实现下 package com.example.conditionvariable;import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;import android.os.Bundle; import android.os.ConditionVariable; import android.support.v7.app.ActionBarActivity; import android.util.Log;public class MainActivity extends ActionBarActivity {public static final String TAG ConditionVariable_Test;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mCondition new ConditionVariable(true);for (int i 0; i 10; i) {new Mythread( i).start();}}public int num 5;class Mythread extends Thread {String name;public Mythread(String name) {this.name name;}Overridepublic void run() {mCondition.block();mCondition.close();while (true) {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}num--;if (num 0)Log.i(TAG, thread name is: name num is: num);elsebreak;}mCondition.open();}} }运行的结果如下 onditionVariable_Test I thread name is:0 num is:4I thread name is:0 num is:3I thread name is:0 num is:2I thread name is:0 num is:1I thread name is:0 num is:0很明显这是我想要的效果还有其它办法吗当然有 我们还可以使用ReentrantLock重入锁代码修改如下 public class MainActivity extends ActionBarActivity {public static final String TAG ConditionVariable_Test;private Lock lock new ReentrantLock();Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);for (int i 0; i 10; i) {new Mythread( i).start();}}public int num 5;class Mythread extends Thread {String name;public Mythread(String name) {this.name name;}Overridepublic void run() {lock.lock();while (true) {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}num--;if (num 0)Log.i(TAG, thread name is: name num is: num);elsebreak;}lock.unlock();}} } 运行的结果如下 onditionVariable_Test I thread name is:0 num is:4I thread name is:0 num is:3I thread name is:0 num is:2I thread name is:0 num is:1I thread name is:0 num is:0很明显这是我想要的效果还有其它办法吗当然有那就是用synchronized同步块代码改成如下 package com.example.conditionvariable;import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;import android.os.Bundle; import android.os.ConditionVariable; import android.support.v7.app.ActionBarActivity; import android.util.Log;public class MainActivity extends ActionBarActivity {public static final String TAG ConditionVariable_Test;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);for (int i 0; i 10; i) {new Mythread( i).start();}}public int num 5;class Mythread extends Thread {String name;public Mythread(String name) {this.name name;}Overridepublic void run() {synchronized (MainActivity.class) {while (true) {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}num--;if (num 0)Log.i(TAG, thread name is: name num is: num);elsebreak;}}}} }运行的结果如下 onditionVariable_Test I thread name is:0 num is:4I thread name is:0 num is:3I thread name is:0 num is:2I thread name is:0 num is:1I thread name is:0 num is:0很明显这是我想要的效果 五、总结 在Android开发里面我们一般实现线程通过可以用ConditionVariable、ReentrantLock(重入锁)、synchronized、阻塞队列(ArrayBlockingQueue、LinkedBlockingQueue)    put(E e) : 在队尾添加一个元素如果队列满则阻塞    size() : 返回队列中的元素个数    take() : 移除并返回队头元素如果队列空则阻塞
http://www.lebaoying.cn/news/70070.html

相关文章:

  • led灯网站模板公司名称注册查询官网入口
  • 哪里可以做网站啊成都公司网站制作公司
  • 域名转出过程网站能打开吗深圳福田专业网站改版
  • 兼职做诚信网站认证seo推广沧州公司电话
  • 邢台企业手机网站建设虎丘做网站价格
  • 做天猫网站价格表律师怎么做网站
  • 定制网站系统做购物网站的初衷
  • 建一个网站需要哪些人厦门网站设计培训公司
  • 在哪里可以免费做个人网站免费资料网站网址下载
  • 网站开发工程师怎么考wordpress网站如何与关联
  • 网站建设工单系统护语做视频链接的网站
  • 无锡整站百度快照优化西安seo主管
  • 中国建设投资集团 网站首页网站建设整体设计思路
  • php网站的优势国内oa系统十大排名
  • 网站建设 知识产权苏州高新区建设局网站管网
  • 网站建设时间如何查询接做网站的私活怎么报价
  • 淄博微网站建设淘宝电脑版登录入口
  • 南宁网站排名优化公司哪家好网站备案繁琐工作
  • 专业网站制作技术镇江网络科技有限公司
  • 淘宝可做的团购网站html网站用什么空间
  • 正规品牌网站设计图片制作网页可以用
  • 百度推广让我先做虚拟网站后开发公司房子出售怎么不交税
  • 经典手机网站有没有可以做各种字体的网站
  • 可拖拽式网站建设上海抓捕236名
  • 公司网站建设方案合肥网站制作建设公司
  • 学做网站都要学什么专业免费的app软件下载安装
  • dz网站收款即时到账怎么做的网站后台模板 下载
  • 品牌推广网站如何做北京高端网站建设公司浩森宇特
  • 抚州律师网站建设企业管理有限公司经营范围有哪些
  • 怎么制作网站的链接宏住房和城乡规划建设局网站