拓者吧室内设计网站门户网站维护

bicheng/2026/1/25 12:28:18/文章来源:
拓者吧室内设计网站,门户网站维护,中文网站模板html,wordpress 4.8上传漏洞一、学习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.mzph.cn/bicheng/86869.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

申请自助建站安徽茶叶网站建设

题目: 字符串里里面字符出现的次数和出现哪些不同的字符 such as 字符串“aaaabbbccd” 打印出出现a4次,b3次,c2次,d1次,出现的不同字符的字符串为“abcd”,或者按照规则打印字符串“4a3b2c1d” 代码: #include <stdio.h> #include <stdlib.h> #include &l…

空间排版设计网站添加图标wordpress

晚餐时间马上就到了&#xff0c;奶牛们还在各自的牧场中悠闲的散着步。 当农夫约翰摇动铃铛&#xff0c;这些牛就要赶回牛棚去吃晚餐。 在吃晚餐之前&#xff0c;所有奶牛都在自己的牧场之中&#xff0c;有些牧场中可能没有奶牛。 每个牧场都通过一条条道路连接到一个或多个…

企业是做app还是做网站免费建站网站一级123456

Autosar_BSW的Diagnostics功能 一、Autosar_BSW的Diagnostics功能 1、Diagnostics组件图 2、架构与术语解释 3、工作流程

网站建设与维护题库及答案企业进行网站建设的方式

stc8H驱动并控制三相无刷电机综合项目技术资料综合篇 🌿相关项目介绍《基于stc8H驱动三相无刷电机开源项目技术专题概要》 🔨停机状态,才能进入设置状态,可以设置调速模式,以及转动方向。 ✨所有的功能基本已经完成调试,目前所想到的功能基本已经都添加和实现。引脚利…

优惠券网站做代理怎么样免费设计图片软件

FROM_UNIXTIME()、UNIX_TIMESTAMP()和CONVERT_TZ()的64位支持 根据MySQL 8.0.28版本的更新&#xff0c;FROM_UNIXTIME()、UNIX_TIMESTAMP() 和 CONVERT_TZ() 函数现在在支持64位的平台上处理64位值。这包括64位版本的Linux、MacOS和Windows。在兼容的平台上&#xff0c;UNIX_T…

网站维护外包合同济宁做网站的公司

最后一公里&#xff0c;出自中国共产党十八大以来的新名词之一&#xff0c;指政策始终“走在路上”&#xff0c;服务始终“停在嘴上”&#xff0c;实惠没有真正“落在身上”的“末梢堵塞”问题。要让人民群众真正得实惠&#xff0c;就要切实解决好“最后一公里”问题。1、移动互…

企业还做网站吗桂林两江四湖景区

P2495 [SDOI2011]消耗战 代码 有的虚树建立好像把一些点没建&#xff0c;他们不用判断是否是关键点&#xff1b; il void push(int x) {if(t 1) {s[ t] x;return;}int l lca(x, s[t]); if(l s[t]) return; //这句话我没看懂&#xff0c;因该就是这&#xff0c;脑子好乱&a…

网站ui设计兼职做门面商铺比较好的网站

最近护眼台灯的热度真是不小&#xff0c;许多博主纷纷推荐。考虑到孩子即将放寒假&#xff0c;市场上的产品也是五花八门&#xff0c;于是我决定认真研究一下&#xff0c;找出其中的水货和宝藏产品。我挑选了市场上口碑较好的3款产品进行深入评估&#xff0c;主要从照度、显色指…

烟台网站建设学校网站开发需求描述

随着城市化进程的不断加速&#xff0c;地铁作为一种便捷、快速的城市交通方式&#xff0c;受到了越来越多人的青睐。地铁列车可视化&#xff0c;作为地铁运营管理中的一项重要工作&#xff0c;不仅可以提高列车运行效率和安全性&#xff0c;还可以为乘客提供更加舒适、便捷的乘…

做恒指网站陕西省交通建设集团网站

Java解决查找包含给定字符的单词 01 题目 给你一个下标从 0 开始的字符串数组 words 和一个字符 x 。 请你返回一个 下标数组 &#xff0c;表示下标在数组中对应的单词包含字符 x 。 注意 &#xff0c;返回的数组可以是 任意 顺序。 示例 1&#xff1a; 输入&#xff1a;…

网络营销管理商丘seo快速排名

个人简介 &#x1f440;个人主页&#xff1a; 前端杂货铺 ⚡开源项目&#xff1a; rich-vue3 &#xff08;基于 Vue3 TS Pinia Element Plus Spring全家桶 MySQL&#xff09; &#x1f64b;‍♂️学习方向&#xff1a; 主攻前端方向&#xff0c;正逐渐往全干发展 &#x1…

网站首页怎么做全屏swf建设大型网站设计公司

指针 指针是什么 数据在内存中存放的方式 声明一个变量int i 3;&#xff0c;那么在内存中就会分配一个大小为4字节&#xff08;因为int类型占4字节&#xff09;的内存空间给变量i&#xff0c;这块内存空间存放的数据就是变量i的值。 换句话说就是&#xff0c;在内存中给变…

网站被镜像怎么做重庆最新重大新闻

Python 安装与环境配置 1. Python 安装 版本推荐 3.10.0下载地址&#xff1a;www.python.org/downloads/w… 若需要安装旧版本&#xff0c;在页面下方选择对应版本即可&#xff0c;MacOS选择对应系统即可 图示下载windows 3.11.4版本 安装Python 执行安装程序&#xff0c;安…

合肥网站开发建设辽宁建网站

离线程序 https://gitcode.net/zengliguang/ky10_aarch64_openssl_install.git 输入下面命令执行离线安装脚本 source openssl_offline_install.sh 安装完成

上海网站建设咨询长沙市公司

文章目录 1、outfile写一句话2、general_log_file写一句话 通过弱口令拿到进到phpMyAdmin页面如何才能获取权限 1、outfile写一句话 尝试执行outfile语句写入一句话木马 select "<?php eval($_REQUEST[6868])?>" into outfile "C:\\phpStudy\\WWW\\p…

网上购物有哪些网站?wordpress获取登录权限

问题描述&#xff1a; 导入自己定义的模块时&#xff0c;出现红色波浪线&#xff0c;可以继续执行 解决&#xff1a; 在存放当前执行文件的文件夹右键&#xff0c;然后将其设置为sources root即可 结果&#xff1a;

酒店建筑设计网站灯光设计公司排名

Navicat Premium&#xff08;16.3.3 Windows 版或以上&#xff09;正式支持 GaussDB 分布式数据库。GaussDB 分布式模式更适合对系统可用性和数据处理能力要求较高的场景。Navicat 工具不仅提供可视化数据查看和编辑功能&#xff0c;还提供强大的高阶功能&#xff08;如模型、结…

linux网站建设高端互联网推广

Anaconda常用命令 文章目录 Anaconda常用命令1. 前言2. 管理conda自身2.1 查看conda版本2.2 查看conda的环境配置2.3 设置镜像2.4 更新conda2.6 更新Anaconda整体2.7 查询某个命令的帮助 3. 管理环境3.1 创建虚拟环境3.2 创建虚拟环境的同时安装必要的包3.3 查看有哪些虚拟环境…

在excel中怎么做邮箱网站h5制作免费素材

本文内容 WPF 中的拖放支持数据传输拖放事件实现拖放拖放示例 本主题概述 Windows Presentation Foundation (WPF) 应用程序中的拖放支持。 拖放通常指一种数据传输方法&#xff1a;使用鼠标&#xff08;或一些其他指针设备&#xff09;选择一个或多个对象&#xff0c;将其拖…

国内十大旅游网站排名营销网站建设阿凡达

当下&#xff0c;传统的图文电商模式已经走向没落&#xff0c;以抖音为首的直播电商模式备受用户追捧&#xff0c;它具有实时直播和强互动的特点&#xff0c;是传统电商所不具备的优势。而且&#xff0c;当前正是直播电商的红利期&#xff0c;很多主播和品牌商都通过直播电商业…