nginx 使用的 epoll 是非阻塞的,但不是异步IO。 glibc 的 aio 有 bug , kernel 的 aio 只能以 O_DIRECT 方式做直接 IO , libeio 也是 beta 阶段。epoll 是成熟的,但是 epoll 本身是同步的。Linux 上目前没有像 IOCP 这样的成熟异步 IO 实现。
线程更为轻量,切换起来比进程消耗资源少 并发与并行概念 并发(concurrent):同一时间应对(dealing with)多件事情的能力 并行(parallel):同一时间动手(doing)做多件事情的能力
package java.util.concurrent; import java.util.concurrent.locks.AbstractQueuedSynchronizer; public class
首先实现线程安全的queue的方式很很多种,比如说方法全部加锁,再或者使用基于CAS的LinkedQueue。然后ConcurrentLinkedQueue是一个无界线程安全队列(当然也遵循先进先出)
看一下demo: import java.util.concurrent.Exchanger; public class ExchangerDemo { static Exchanger exchanger
在说Executor前, 先来看一下线程创建的几种方式: 1、继承Thread类创建线程 2、 实现Runable接口创建线程 3、使用Callable和Future 创建线程 4、使用Executor线程池 这几种方式是存在一定程度上的差异,首先Thread则是最原始的,创建线程执行对应的业务代码,Runable是完成了一个任务的的执行,然后Callable和Runable类似,但是提供了Future来实现了一种回调方式。这三种方式总体来说是比较原始的,线程无法复用,线程及任务管理复杂。而Executor 了为了解决这些类似的问题而实现的。
例如一个整数在多线程下的自增 package com.jmy.concurrent; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock 例如一场面试要等面试官到场才可以开始 ---- package com.jmy.concurrent; import java.util.concurrent.CountDownLatch; public CyclicBarrier CyclicBarrier被称为栅栏 现场所有线程都到达指定地点之后在一起执行 例如所有运动员都到达起跑线发令枪响之后一起跑了出去 ---- package com.jmy.concurrent ; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; public class CyclicBarrierDemo { public static void main(String
https://greenlet.readthedocs.io/en/latest/
Java Concurrent 包并发编程中通用的工具类。包括一些标准化,可扩展的小的实现结构(框架),和一些实用但却枯燥且不易实现的工具类。以下为主要组件的简易描述。 另参考 java.util.concurrent.locks 和 java.util.atomic 包。 以上几个类中以 “Concurrent(并发)” 作为前缀意在与类似的 “synchronized” 类进行区分。 并发包(java.util.concurrent)及其子包中所有类方法沿用以上规则并扩展至更高级别的同步机制。 java.util.concurrent.atomic支持对单变量无锁且线程安全地操作的工具包java.util.concurrent.locks包括了对锁和等待机制实现的类和接口,有别于内置的同步器和监视器
demo import java.util.concurrent.CyclicBarrier; public class CyclicBarrierTest { CyclicBarrier c
死锁是一个比较大的概念,在并发场景下的加锁行为都有可能产生死锁问题。在Java 并发编程中会有死锁,操作系统里也有死锁,数据库里也见过死锁,分布式里也有死锁, 看上去蛮常见的,这一篇主要简单的介绍下死锁,然后说一说在并发编程中如何对待死锁。
HashMap是我们生产过程中使用较多的一个数据结构,平时非并发场景使用的HashMap,并发场景下使用的HashTable、ConcurrentHashMap。
看concurrent包中的源码时,volatile是必须要理解的。
Semaphore一种通常用于维持数量的信号量工具,在Java 并发中最常见的使用就是维持并发线程的个数,正如之前提到的,并不是并发场景线程越多越好,应该是跟对应的业务场景及CPU数量共同决定。而需要维持数量时,我们大致就需要一个JUC里面Semaphore这样一个信号量工具。Semaphore 同样依赖于AQS实现(shared),内部持有一个Sync对象,同样存在公平实现、非公平实现两种模式(可以在构造时指定fair参数)。
variable corresponding to each declared local variable in a module instance, regardless of the number of concurrent Verilog 标准提供的 Automatic Task,在标准中是这样描述的: All variables of an automatic task shall be replicated on each concurrent an automatic task that is re-entrant with all the task declarations allocated dynamically for each concurrent
1/ concurrent.futures模块 线程池:concurrent.futures.ThreadPoolExecutor(max_workers) 进程池:concurrent.futures.ProcessPoolExecutor (max_workers) 2、使用对比:进程的性能更好 import concurrent.futures import time number_list = [1, 2, 3, 4, 5, execution in " + str(time.time() - start_time), "seconds") # 线程池执行 start_time_1 = time.time() with concurrent.futures.ThreadPoolExecutor executor: futures = [executor.submit(evaluate_item, item) for item in number_list] for future in concurrent.futures.as_completed executor: futures = [executor.submit(evaluate_item, item) for item in number_list] for future in concurrent.futures.as_completed
而concurrent.futures模块,可以利用multiprocessing实现真正的平行计算。 核心原理是:concurrent.futures会以子进程的形式,平行的运行多个python解释器,从而令python程序可以利用多核CPU来提升执行速度。 第一章 concurrent.futures性能阐述 最大公约数 这个函数是一个计算密集型的函数。 多线程ThreadPoolExecutor import time from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor 第二章 concurrent.futures源码分析 Executor 可以任务Executor是一个抽象类,提供了如下抽象方法submit,map(上面已经使用过),shutdown。
java.util.concurrent简介 java.util.concurrent包提供了很多有用的类,方便我们进行并发程序的开发。本文将会做一个总体的简单介绍。 主要的组件 java.util.concurrent包含了很多内容, 本文将会挑选其中常用的一些类来进行大概的说明: Executor ExecutorService ScheduledExecutorService return t; } } 本文的例子可以参考https://github.com/ddean2009/learn-java-concurrency/tree/master/concurrent-overview
引言 最近看一些代码的时候,发现有人用 System.Collections.Concurrent 下的 BlockingCollection 很便利的实现了生产者 - 消费者模式,这是之前没有注意到的 ConcurrentStack<T> 或 ConcurrentBag<T>这些并发队列,并发堆栈,并发包相关的使用,正好好奇 BlockingCollection的用法,本次将 System.Collections.Concurrent 简介 那先来看一下该库都有哪些成员从微软官方文档看一下 System.Collections.Concurrent[1]的介绍: 共有如下成员,类成员包含: BlockingCollection<T>: 总结 上述就是对 System.Collections.Concurrent 命名空间的所有成员做了比较详细的说明和应用了,我们可以通过合理选择这些集合类,可以显著简化多线程编程,同时保证高性能与线程安全 参考链接 [1] https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent?view=net-9.0
在这些任务中,往往需要生成线程池,concurrent.futures模块对threading和multiprocessing模块进行了进一步的包装,可以很方便地实现池的功能。 下载 python3中concurrent.futures是标准库,在python2中还需要自己安装futures: pip install futures Executor与Future concurrent.futures from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor import requests def load_url( from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, wait, ALL_COMPLETED, FIRST_COMPLETED from concurrent.futures import as_completed import requests URLS = ['http://httpbin.org', 'http://example.com