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

长春市长春网站建设网seo公司服务

长春市长春网站建设网,seo公司服务,网站建设案例基本流程图,睢县做网站哪家好Message中可以携带的信息 Message中可以携带的数据比较丰富,下面对一些常用的数据进行了分析。 /*** 用户定义的消息代码,以便当接受到消息是关于什么的。其中每个Hanler都有自己的命名控件,不用担心会冲突*/ public int what; /*** 如果你…

Message中可以携带的信息

Message中可以携带的数据比较丰富,下面对一些常用的数据进行了分析。

/*** 用户定义的消息代码,以便当接受到消息是关于什么的。其中每个Hanler都有自己的命名控件,不用担心会冲突*/ public int what;
/*** 如果你只想存很少的整形数据,那么可以考虑使用arg1与arg2,* 如果需要传输很多数据可以使用Message中的setData(Bundle bundle)*/public int arg1;
/*** 如果你只想存很少的整形数据,那么可以考虑使用arg1与arg2,* 如果需要传输很多数据可以使用Message中的setData(Bundle bundle)*/public int arg2;
/*** 发送给接受方的任意对象,在使用跨进程的时候要注意obj不能为null*/public Object obj;
/*** 在使用跨进程通信Messenger时,可以确定需要谁来接收*/public Messenger replyTo;
/*** 在使用跨进程通信Messenger时,可以确定需要发消息的uid*/public int sendingUid = -1;
/*** 如果数据比较多,可以直接使用Bundle进行数据的传递*/Bundle data;

创建消息的方式

官方建议使用Message.obtain()系列方法来获取Message实例,因为其Message实例是直接从Handler的消息池中获取的,可以循环利用,不必另外开辟内存空间,效率比直接使用new Message()创建实例要高。其中具体创建消息的方式,我已经为大家分好类了。具体分类如下:

//无参数
public static Message obtain() {...}
//带Messag参数
public static Message obtain(Message orig) {}
//带Handler参数
public static Message obtain(Handler h) {}
public static Message obtain(Handler h, Runnable callback){}
public static Message obtain(Handler h, int what){}
public static Message obtain(Handler h, int what, Object obj){}
public static Message obtain(Handler h, int what, int arg1, int arg2){}
public static Message obtain(Handler h, int what,int arg1, int arg2, Object obj) {}

其中在Message的obtain带参数的方法中,内部都会调用无参的obtain()方法来获取消息后。然后并根据其传入的参数,对Message进行赋值。(关于具体的obtain方法会在下方消息池实现原理中具体描述)

消息池实现原理

 既然官方建议使用消息池来获取消息,那么在了解其内部机制之前,我们来看看Message中的消息池的设计。具体代码如下:

private static final Object sPoolSync = new Object();//控制获取从消息池中获取消息。保证线程安全
private static Message sPool;//消息池
private static int sPoolSize = 0;//消息池中回收的消息数量
private static final int MAX_POOL_SIZE = 50;//消息池最大容量
// sometimes we store linked lists of these things@UnsupportedAppUsage/*package*/ Message next;public static Message obtain() {synchronized (sPoolSync) {if (sPool != null) {Message m = sPool;sPool = m.next;m.next = null;m.flags = 0; //重新标识当前Message没有使用过sPoolSize--;return m;}}return new Message();//如果为空直接返回}

从中我们发现如果sPool如果不为null,则返回直接new一个Message返回,整个方法结束,那么sPool是什么,sPool是一个message,从源码中我们可以发现sPool其实就相当于一个头指针,指向缓存池中第一个缓存的Message,如果sPool不为null则说明缓存池中存在空闲的Message,返回缓存池中空闲的message,然后sPool执行下一个缓存message对象,然后将msg.next重置为0,整体代码过下来,我们发现Message的缓存池其实就是用了一个数据结构-单向链表。具体流程如图:

这块就很明显是消息池的取出了,那么它的存是在哪里呢,全局搜sPool,我们发现在recycleUncheck中有实现

   /*** Recycles a Message that may be in-use.* Used internally by the MessageQueue and Looper when disposing of queued Messages.*/@UnsupportedAppUsagevoid recycleUnchecked() {// Mark the message as in use while it remains in the recycled object pool.// Clear out all other details.flags = FLAG_IN_USE;what = 0;arg1 = 0;arg2 = 0;obj = null;replyTo = null;sendingUid = UID_NONE;workSourceUid = UID_NONE;when = 0;target = null;callback = null;data = null;synchronized (sPoolSync) {if (sPoolSize < MAX_POOL_SIZE) {next = sPool;sPool = this;sPoolSize++;}}}

然后这块代码又是在recycle中调用的

 /*** Return a Message instance to the global pool.* <p>* You MUST NOT touch the Message after calling this function because it has* effectively been freed.  It is an error to recycle a message that is currently* enqueued or that is in the process of being delivered to a Handler.* </p>*/public void recycle() {if (isInUse()) {if (gCheckRecycle) {throw new IllegalStateException("This message cannot be recycled because it "+ "is still in use.");}return;}recycleUnchecked();}

这块首先判断消息是否在使用之中,如果在使用之中,继续判断gCheckRecycle,gCheckRecycle的默认值是true,如果不在使用之中,最后会走进recycleUnchecked。

然后来分析recycleUnChecked

一开始,把这个消息所有成员赋值成最初的状态,FLAG_IN_USE的值是1一开始说了Message的flags表示这个Message有没有在使用,1表示在池中,等待复用,0表示正在被使用。重点看同步锁中的代码。

假设全局池没有元素时,我们将第一个消息放到池中,sPool一开始是NULL,next指向了sPool,所以此时的消息的sPool和next都是NULL,然后sPool指向当前的Message对象,最后池的数量加1。大致如下图。

假设有来个消息m2,在走一遍同步锁中的代码,此时全局池的状态如下图所示。

其他几个类似

http://www.shuangfujiaoyu.com/news/46210.html

相关文章:

  • 地方门户网站如何推广企业qq官网
  • 网站域名使用怎么做分录郑州百度seo
  • 网站建设中可能出现的问题上海seo公司哪个靠谱
  • 网站建设总流程百度推广退款电话
  • 电脑网站素材三明网站seo
  • 台州做网站优化自媒体发稿
  • 双线网站核心关键词和长尾关键词举例
  • 中国佛山手机网站建设百度seo报价方法
  • dreamwear做网站现在疫情怎么样了最新消息
  • 网站的运行与维护竞价推广托管开户
  • 邮编域名做网站新闻头条今日新闻
  • 临沂个人做网站如何优化标题关键词
  • 企业网站个人备案吗安卓手机优化大师官方下载
  • 做问卷调查赚钱的网站seo服务公司招聘
  • 家政服务公司网站建设方案策划书游戏代理平台
  • 深圳网站建设企业app运营方案策划
  • 现在lol谁做教学视频网站青岛seo优化公司
  • 只做画册的网站怎么免费创建网站
  • 浦东网站建设公司优化关键词的作用
  • 国内做微商城比较知名的网站培训学校网站
  • 做app需要什么软件seo自然排名关键词来源的优缺点
  • 橙子建站广告怎么做个人发布信息免费推广平台
  • 免费注册163免费邮箱申请长春关键词优化公司
  • 美国政府网站建设友情链接你会回来感谢我
  • 东莞网站建设关键词百度云网盘资源分享网站
  • 网站设计电商运营网站排名查询
  • 电商平台网站模板百度推广下载
  • 网站审核要多久深圳seo推广外包
  • 深圳华强北赛格大厦最新消息seo排名需要多少钱
  • 域名暂无法进行网站备案系统优化软件推荐