Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 48|回复: 0

Zebra项目知识介绍共五篇(二)

[复制链接]

1万

主题

1万

帖子

5万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
58026
发表于 2020-10-20 11:42:33 | 显示全部楼层 |阅读模式

                    

                    

                    
                    
                    <p style="text-align: center;"><span style="color: rgb(0, 209, 0);">Zebra项目知识介绍共五篇(二)——</span></p><p style="text-align: center;"><span style="color: rgb(0, 209, 0);">BlockingQueue 、 CountDownLatch 、 ExecutorService_Lock 、 GoogleProtoBuffer</span></p><p>===========================================================</p><p><span style="color: rgb(255, 79, 121);">一、BlockingQueue - 阻塞式队列</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>ArrayBlockingQueue</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>LinkedBlockingQueue</p><p><br  /></p><p><span style="color: rgb(255, 79, 121);">二、CountDownLatch - 闭锁、线程递减锁</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>需要在创建时指定一个数字</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>可以同构await方法产生要给阻塞,直到数字减为0时,阻塞自动被放开</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>可以通过countDown方法使数字减减</p><p><br  /></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>通常用来实现一个线程的执行需要等待其他多个线程执行完成的场景</p><p><br  /></p><p><span style="color: rgb(255, 79, 121);">三、ConcurrentMap</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>可以在并发场景下提供线程安全的Map,和HashTable很像,但是能够提供更好的性能。</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>HashTable一旦锁定锁定的是整个Map</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>而ConcurrentHashMap在锁定时自锁定正在操作的部分,大大的减少了冲突的可能,自然效率就高</p><p><br  /></p><p><span style="color: rgb(255, 79, 121);">四、CyclicBarrier - 栅栏</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>适用于希望多个线程在某一个节点找齐,当指定数量的线程都到达该节点时再同时放行接着去执行</p><p><br  /></p><p><span style="color: rgb(255, 79, 121);">五、Exchanger - 交换机</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>适用于两个线程需要交换对象的场景</p><p><br  /></p><p><span style="color: rgb(255, 79, 121);">六、Semaphore - 信号量</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span></p><p><br  /></p><p><span style="color: rgb(255, 79, 121);">七、ExecutorService 执行器服务 - ThreadPoolExecutor 线程池执行器</span></p><p><span style="color: rgb(0, 82, 255);">1.手动配置参数创建线程池</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 5, 5, TimeUnit.SECONDS, new ArrayBlockingQueue&lt;Runnable&gt;(3), new RejectedExecutionHandler() {</p><p><span class="Apple-tab-span" style="white-space:pre;">                </span>@Override</p><p><span class="Apple-tab-span" style="white-space:pre;">                </span>public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {</p><p><span class="Apple-tab-span" style="white-space:pre;">                        </span>System.out.println("实在处理不了了,放弃他吧:"+r.toString());</p><p><span class="Apple-tab-span" style="white-space:pre;">                        </span>executor.remove(r);</p><p><span class="Apple-tab-span" style="white-space:pre;">                </span>}</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>});</p><p><br  /></p><p><br  /></p><p><span style="color: rgb(0, 82, 255);">2.通过Executors工具类来获取采用通用配置的线程池</span><span class="Apple-tab-span" style="white-space:pre;">        </span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>Executors:</p><p><br  /></p><p><span class="Apple-tab-span" style="white-space:pre;">                </span>newCachedThreadPool() // 创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们。对于执行很多短期异步任务的程序而言,这些线程池通常可提高程序性能。</p><p><br  /></p><p><span class="Apple-tab-span" style="white-space:pre;">                </span>newFixedThreadPool(int nThreads) //创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程。在任意点,在大多数 nThreads 线程会处于处理任务的活动状态。如果在所有线程处于活动状态时提交附加任务,则在有可用线程之前,附加任务将在队列中等待。</p><p><br  /></p><p><span class="Apple-tab-span" style="white-space:pre;">                </span>newSingleThreadExecutor() //创建一个使用单个 worker 线程的 Executor,以无界队列方式来运行该线程。</p><p><br  /></p><p><span style="color: rgb(0, 82, 255);">3.使用线程池</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>execute(Runnable)</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>submit(Runnable)</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>submit(Callable)</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>invokeAny(...)</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>invokeAll(...)</p><p><br  /></p><p><br  /></p><p><span style="color: rgb(0, 82, 255);">4.关闭线程池</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>shutdown()</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>shutdownNow()</p><p><br  /></p><p><span style="color: rgb(0, 82, 255);">5.其他ExecutorService</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>ScheduledExecutorService它能够将任务延后执行,或者间隔固定时间多次执行</p><p><br  /></p><p><br  /></p><p><span style="color: rgb(255, 79, 121);">八、Lock - 锁</span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>可以用来替代同步代码块,用起来更加方便<span class="Apple-tab-span" style="white-space: pre;">        </span></p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>ReentrantLock</p><p><span class="Apple-tab-span" style="white-space:pre;">        </span>ReadWriteLock</p><p><br  /></p><p><span style="color: rgb(255, 79, 121);">九、原子性操作</span></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Atomic<br  /></p><p><span class="Apple-tab-span" style="white-space: pre;">        </span><br  /></p><p style="font-size: 16px;white-space: normal;max-width: 100%;min-height: 1em;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-family: 宋体;font-size: 14px;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></span></span></p><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><hr style="font-size: 16px;white-space: normal;max-width: 100%;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"  /><p style="font-size: 16px;white-space: normal;max-width: 100%;min-height: 1em;color: rgb(62, 62, 62);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;line-height: 25.6px;color: rgb(0, 128, 255);box-sizing: border-box !important;word-wrap: break-word !important;">请扫描下面二维码,关注该微信公众号,获取更多</span><span style="max-width: 100%;line-height: 25.6px;color: rgb(255, 41, 65);box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">生物医学工程</strong>专业及<strong style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">医工</strong><span style="max-width: 100%;line-height: 25.6px;color: rgb(0, 128, 255);box-sizing: border-box !important;word-wrap: break-word !important;">学习笔记</span>:</span><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;word-wrap: break-word !important;"></span></p><p style="font-size: 16px;white-space: normal;max-width: 100%;min-height: 1em;color: rgb(62, 62, 62);line-height: 25.6px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;line-height: 25.6px;color: rgb(255, 41, 65);box-sizing: border-box !important;word-wrap: break-word !important;"></span></p><p style="font-size: 16px;white-space: normal;max-width: 100%;min-height: 1em;color: rgb(62, 62, 62);line-height: 25.6px;text-align: center;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><img class="" data-ratio="1" data-s="300,640" src="http://mmbiz.qpic.cn/mmbiz_jpg/LKgI6UN8ElZnh0jXF3PjbBz7BBPnYH7wbDgfc3E2r7REMuN2lQqfuVM38HZO8Zx0rN0OLKywrjoH3P7QqDY44Q/640?wx_fmt=jpeg" data-type="jpeg" data-w="430" width="auto" style="box-sizing: border-box !important;word-wrap: break-word !important;visibility: visible !important;width: auto !important;"  /></p><p><br  /></p>
               
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Comsenz Inc. ( 浙ICP备17000336号-1 )

GMT+8, 2025-3-11 01:24 , Processed in 0.078405 second(s), 34 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表