Android中的Semaphore
Posted 孤独的猫咪神
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android中的Semaphore相关的知识,希望对你有一定的参考价值。
信号量,了解过操作系统的人都知道,信号量是用来做什么的···
在android中,已经提供了Semaphore来帮助我们使用~
那么,在开发中这家伙有什么用呢?
用的地方不多,但是却真的是好用至极!
#0 举个栗子
我相信很多人在开发中都会遇到这样的事情:当要对一个资源进行多次数据读取并且修改操作时,会遇到执行的速度快慢不一致导致修改值发生改变的情况。
比如如下代码:
1 private void Start() { 2 for (int i = 10; i >= 0; i--) { 3 LogString = "num:" + i; 4 printLogString(i); 5 } 6 } 7 8 private void printLogString(final int delayTime) { 9 Log.e("TAG", "Runnable" + delayTime); 10 new Thread(new Runnable() { 11 @Override 12 public void run() { 13 Log.e("TAG", LogString); 14 } 15 }).start(); 16 }
打印出的日志会不一致,如:
04-03 15:33:07.294 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable10 04-03 15:33:07.296 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable9 04-03 15:33:07.296 17078-17777/com.gao.luna.semaphoretest E/TAG: num:9 04-03 15:33:07.297 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable8 04-03 15:33:07.297 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable7 04-03 15:33:07.298 17078-17778/com.gao.luna.semaphoretest E/TAG: num:7 04-03 15:33:07.299 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable6 04-03 15:33:07.299 17078-17779/com.gao.luna.semaphoretest E/TAG: num:6 04-03 15:33:07.300 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable5 04-03 15:33:07.301 17078-17780/com.gao.luna.semaphoretest E/TAG: num:5 04-03 15:33:07.301 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable4 04-03 15:33:07.302 17078-17781/com.gao.luna.semaphoretest E/TAG: num:4 04-03 15:33:07.303 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable3 04-03 15:33:07.303 17078-17782/com.gao.luna.semaphoretest E/TAG: num:3 04-03 15:33:07.304 17078-17783/com.gao.luna.semaphoretest E/TAG: num:3 04-03 15:33:07.305 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable2 04-03 15:33:07.305 17078-17784/com.gao.luna.semaphoretest E/TAG: num:2 04-03 15:33:07.305 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable1 04-03 15:33:07.306 17078-17078/com.gao.luna.semaphoretest E/TAG: Runnable0 04-03 15:33:07.306 17078-17785/com.gao.luna.semaphoretest E/TAG: num:0 04-03 15:33:07.308 17078-17786/com.gao.luna.semaphoretest E/TAG: num:0 04-03 15:33:07.308 17078-17787/com.gao.luna.semaphoretest E/TAG: num:0
这和我们希望见到的num从10打印到0时不相符的。
#1 Semaphore的使用
接着上面的代码,我们加入Semaphore来解决这个问题:
1 private final Semaphore semaphore = new Semaphore(0, true);
这里有个坑需要注意一下,比如说这个队列限制只有1个存在,那么在创建时要写0,写1的话,就会变成2个了···以此类推,要队列中有10个对象,需要写9···
在每次acquire后,semaphone将加入一个对象,在不release的情况下,这个对象是会一直存在的。
下面上代码:
1 private final Semaphore semaphore = new Semaphore(0, true); 2 3 private void Start() { 4 for (int i = 10; i >= 0; i--) { 5 try { 6 Log.e("Semaphore", "semaphore.acquire();" + i); 7 semaphore.acquire(); 8 } catch (Exception ex) { 9 Log.e("Semaphore", ex.getMessage()); 10 } 11 LogString = "num:" + i; 12 printLogString(i); 13 } 14 } 15 16 private void printLogString(final int delayTime) { 17 new Thread(new Runnable() { 18 @Override 19 public void run() { 20 Log.e("Semaphore", "semaphore.release();"); 21 Log.e("TAG", LogString); 22 semaphore.release(); 23 } 24 }).start(); 25 }
日志如下:
04-03 15:40:41.060 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.065 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();10 04-03 15:40:41.078 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();9 04-03 15:40:41.078 26200-26513/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.084 26200-26513/com.gao.luna.semaphoretest E/TAG: num:10 04-03 15:40:41.086 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();8 04-03 15:40:41.086 26200-26514/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.086 26200-26514/com.gao.luna.semaphoretest E/TAG: num:9 04-03 15:40:41.086 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();7 04-03 15:40:41.087 26200-26515/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.087 26200-26515/com.gao.luna.semaphoretest E/TAG: num:8 04-03 15:40:41.087 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();6 04-03 15:40:41.088 26200-26516/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.088 26200-26516/com.gao.luna.semaphoretest E/TAG: num:7 04-03 15:40:41.088 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();5 04-03 15:40:41.088 26200-26517/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.088 26200-26517/com.gao.luna.semaphoretest E/TAG: num:6 04-03 15:40:41.089 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();4 04-03 15:40:41.089 26200-26518/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.089 26200-26518/com.gao.luna.semaphoretest E/TAG: num:5 04-03 15:40:41.090 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();3 04-03 15:40:41.090 26200-26519/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.091 26200-26519/com.gao.luna.semaphoretest E/TAG: num:4 04-03 15:40:41.091 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();2 04-03 15:40:41.092 26200-26520/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.092 26200-26520/com.gao.luna.semaphoretest E/TAG: num:3 04-03 15:40:41.092 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();1 04-03 15:40:41.093 26200-26521/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.093 26200-26521/com.gao.luna.semaphoretest E/TAG: num:2 04-03 15:40:41.094 26200-26200/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();0 04-03 15:40:41.094 26200-26522/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.094 26200-26522/com.gao.luna.semaphoretest E/TAG: num:1 04-03 15:40:41.095 26200-26523/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 15:40:41.095 26200-26523/com.gao.luna.semaphoretest E/TAG: num:0
这里我们可以很清晰的看到,num从10顺序变为了0。
#2 再加点延迟试试
现在,我们再在其中增加一些sleep代码,再来测试看看。
1 private final Semaphore semaphore = new Semaphore(0, true); 2 3 private void Start() { 4 for (int i = 10; i >= 0; i--) { 5 try { 6 Log.e("Semaphore", "semaphore.acquire();" + i); 7 semaphore.acquire(); 8 } catch (Exception ex) { 9 Log.e("Semaphore", ex.getMessage()); 10 } 11 LogString = "num:" + i; 12 printLogString(i); 13 } 14 } 15 16 private void printLogString(final int delayTime) { 17 Log.e("TAG", "Runnable" + delayTime); 18 new Thread(new Runnable() { 19 @Override 20 public void run() { 21 try { 22 Thread.sleep(delayTime * 10); 23 } catch (Exception ex) { 24 Log.e("delayTime", ex.getMessage()); 25 } 26 Log.e("Semaphore", "semaphore.release();"); 27 Log.e("TAG", LogString); 28 semaphore.release(); 29 } 30 }).start(); 31 }
然后看看日志:
04-03 16:15:34.028 18148-18148/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 16:15:34.028 18148-18148/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();10 04-03 16:15:34.028 18148-18148/com.gao.luna.semaphoretest E/TAG: Runnable10 04-03 16:15:34.029 18148-18148/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();9 04-03 16:15:34.129 18148-18230/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 16:15:34.129 18148-18230/com.gao.luna.semaphoretest E/TAG: num:10 04-03 16:15:34.130 18148-18148/com.gao.luna.semaphoretest E/TAG: Runnable9 04-03 16:15:34.130 18148-18148/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();8 04-03 16:15:34.221 18148-18232/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 16:15:34.221 18148-18232/com.gao.luna.semaphoretest E/TAG: num:9 04-03 16:15:34.221 18148-18148/com.gao.luna.semaphoretest E/TAG: Runnable8 04-03 16:15:34.222 18148-18148/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();7 04-03 16:15:34.302 18148-18245/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 16:15:34.302 18148-18245/com.gao.luna.semaphoretest E/TAG: num:8 04-03 16:15:34.303 18148-18148/com.gao.luna.semaphoretest E/TAG: Runnable7 04-03 16:15:34.382 18148-18148/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();6 04-03 16:15:34.453 18148-18251/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 16:15:34.453 18148-18251/com.gao.luna.semaphoretest E/TAG: num:7 04-03 16:15:34.453 18148-18148/com.gao.luna.semaphoretest E/TAG: Runnable6 04-03 16:15:34.462 18148-18148/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();5 04-03 16:15:34.523 18148-18266/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 04-03 16:15:34.523 18148-18266/com.gao.luna.semaphoretest E/TAG: num:6 04-03 16:15:34.523 18148-18148/com.gao.luna.semaphoretest E/TAG: Runnable5 04-03 16:15:34.529 18148-18148/com.gao.luna.semaphoretest E/Semaphore: semaphore.acquire();4 04-03 16:15:34.579 18148-18267/com.gao.luna.semaphoretest E/Semaphore: semaphore.release(); 以上是关于Android中的Semaphore的主要内容,如果未能解决你的问题,请参考以下文章