首页 文章 精选 留言 我的

精选列表

搜索[并发编程],共10009篇文章
优秀的个人博客,低调大师

Java并发编程的艺术(二)——重排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34173549/article/details/79612475 当我们写一个单线程程序时,总以为计算机会一行行地运行代码,然而事实并非如此。 什么是重排序? 重排序指的是编译器、处理器在不改变程序执行结果的前提下,重新排列指令的执行顺序,以达到最佳的运行效率。 重排序分类 重排序分为:编译器重排序 和 处理器重排序。 数据依赖 编译器和处理器并不会随意的改变指令的执行顺序,因为有些指令之间是有依赖关系的,若改变了他们的执行顺序,就会出现错误的结果。因此,编译器和处理器只会对没有依赖关系的指令进行重排序。 数据依赖:若相邻的两条指令访问同一个变量,并且其中有一条指令执行写操作,那么这样的两条指令之间存在数据依赖。对于有数据依赖关系的指令,不会发生重排序。 数据依赖关系总结一下为以下三种情况: 指令 示例 读后写 a=b;b=1; 写后写 a=1;a=2; 写后读 a=1;b=a; as-if-serial 在单线程开发中,程序员不需要知道指令是如何重排序的,只要简单地认为指令是按照顺序依次执行的即可。这就是as-if-serial的语义,即:貌似是串行的。

优秀的个人博客,低调大师

java面试-Java并发编程(二)——重排序

当我们写一个单线程程序时,总以为计算机会一行行地运行代码,然而事实并非如此。 什么是重排序? 重排序指的是编译器、处理器在不改变程序执行结果的前提下,重新排列指令的执行顺序,以达到最佳的运行效率。 重排序分类 重排序分为:编译器重排序 和 处理器重排序。 数据依赖 编译器和处理器并不会随意的改变指令的执行顺序,因为有些指令之间是有依赖关系的,若改变了他们的执行顺序,就会出现错误的结果。因此,编译器和处理器只会对没有依赖关系的指令进行重排序。 数据依赖:若相邻的两条指令访问同一个变量,并且其中有一条指令执行写操作,那么这样的两条指令之间存在数据依赖。对于有数据依赖关系的指令,不会发生重排序。 数据依赖关系总结一下为以下三种情况: 指令 示例 读后写 a=b;b=1; 写后写 a=1;a=2; 写后读 a=1;b=a; as-if-serial 在单线程开发中,程序员不需要知道指令是如何重排序的,只要简单地认为指令是按照顺序依次执行的即可。这就是as-if-serial的语义,即:貌似是串行的。

优秀的个人博客,低调大师

iOS: 并发编程的几个知识点

iOS 多线程问题 查阅的大部分资料都是英文的,整理完毕之后,想翻译成中文,却发现很多名字翻译成中文很难表述清楚。 所以直接把整理好的资料发出来,大家就当顺便学习学习英语。 1. Thread Safe Vs Main Thread Safe Main Thread Safe means only safe execute on main thread; Thread Safe means you can modify on any thread simultaneously; 2. ConditionLock Vs Condition NSCondition A condition variable whose semantics follow those used for POSIX-style conditions. A condition is another type of semaphore that allows threads to signal each other when a certain condition is true.Conditions are typically used to indicate the availability of a resource or to ensure that tasks are performed in a specific order.When a thread tests a condition, it blocks unless that condition is already true. It remains blocked until some other thread explicitly changes and signals the condition. The difference between acondition and a mutex lock is that multiple threads may be permitted access to the condition at the same time.The condition is more of a gatekeeper that lets different threads through the gate depending on some specified criteria. Due to the subtleties involved in implementing operating systems, condition locks are permitted to return with spurious success even if they were not actually signaled by your code.To avoid problems caused by these spurious signals, you should always use a predicate in conjunction with your condition lock. When a thread waits on a condition, the condition object unlocks its lock and blocks the thread. When the condition is signaled, the system wakes up the thread. The condition object then reacquires its lock before returning from thewaitorwaitUntilDate:method. Thus, from the point of view of the thread, it is as if it always held the lock. A boolean predicate is an important part of the semantics of using conditions because of the way signaling works. Signaling a condition does not guarantee that the condition itself is true.Using a predicate ensures that these spurious signals do not cause you to perform work before it is safe to do so. The predicate itself is simply a flag or other variable in your code that you test in order to acquire a Boolean result. Thesemanticsfor using anNSConditionobject is as follows: Lock the condition object. Test a boolean predicate. (This predicate is a boolean flag or other variable in your code that indicates whether it is safe to perform the task protected by the condition.) If the boolean predicate is false, call the condition object’swaitorwaitUntilDate:method to block the thread. Upon returning from these methods, go to step 2 to retest your boolean predicate. (Continue waiting and retesting the predicate until it is true.) If the boolean predicate is true, perform the task. Optionally update any predicates (or signal any conditions) affected by your task. When your task is done, unlock the condition object. lock the condition while (!(boolean_predicate)) { wait on condition } do protected work (optionally, signal or broadcast the condition again or change a predicate value) unlock the condition NSCondition的底层是通过pthread_mutex + pthread_cond_t来实现的。 NSConditionLock A lock that can be associated with specific, user-defined conditions. Using anNSConditionLockobject, you can ensure that a thread can acquire a lockonly if a certain condition is met. AnNSConditionLockobject defines a mutex lock that can be locked and unlocked with specific values. NSConditionLock just support condition with a int, if you want support a custom condition value, you should use NSCondition. 用互斥所能不能实现生产者,消费者模型???答案是: YES 参考资料: https://web.stanford.edu/class/cs140/cgi-bin/lecture.php?topic=locks http://blog.ibireme.com/2016/01/16/spinlock_is_unsafe_in_ios/ https://bestswifter.com/ios-lock/ 3. @synchronized Directive The object passed to the @synchronized directive is aunique identifierused to distinguish the protected block. If you execute the preceding method in two different threads, passing a different object for theanObjparameter on each thread, each would take its lock and continue processing without being blocked by the other. If you pass the same object in both cases, however, one of the threads would acquire the lock first and the other would block until the first thread completed the critical section. Several Common ways to use @synchronized wrong @synchronized(nil) @synchronized(][NSObject all] init]) Exceptions With @synchronized As a precautionary measure, the@synchronizedblock implicitly adds an exception handler to the protected code.This handler automatically releases the mutex in the event that an exception is thrown. This means that in order to use the@synchronizeddirective, you must also enable Objective-C exception handling in your code. If you do not want the additional overhead caused by the implicit exception handler, you should consider using the lock classes. 原理 OBJC_EXPORT int objc_sync_enter(id obj) OBJC_AVAILABLE(10.3, 2.0, 9.0, 1.0); OBJC_EXPORT int objc_sync_exit(id obj) OBJC_AVAILABLE(10.3, 2.0, 9.0, 1.0); @synchronized(obj) { // do work } 会被编译器转换为: @try { objc_sync_enter(obj); // do work } @finally { objc_sync_exit(obj); } Example 结论: 你调用sychronized的每个对象,Objective-C runtime 都会为其分配一个递归锁并存储在哈希表中。 如果在sychronized内部对象被释放或被设为nil看起来都 OK。 注意不要向你的sychronizedblock 传入nil!这将会从代码中移走线程安全。 参考资料 http://rykap.com/objective-c/2015/05/09/synchronized/ http://yulingtianxia.com/blog/2015/11/01/More-than-you-want-to-know-about-synchronized/ https://opensource.apple.com/source/objc4/objc4-646/runtime/objc-sync.mm 4. Runloop Perform selector on a thread 当目标线程runloop未启动时是没有效果的。 启动 Runloop If no input sources or timers are attached to the run loop, this method exits immediately; Manually removing all known input sources and timers from the run loop is not a guarantee that the run loop will exit.macOS can install and remove additional input sources as needed to process requests targeted at the receiver’s thread. Those sources could therefore prevent the run loop from exiting. The Run Loop Sequence of Events Each time you run it, your thread’s run loop processes pending events and generates notifications for any attached observers. The order in which it does this is very specific and is as follows: Notify observers that the run loop has been entered. Notify observers that any ready timers are about to fire. Notify observers that any input sources that are not port based are about to fire. Fire any non-port-based input sources that are ready to fire. If a port-based input source is ready and waiting to fire, process the event immediately. Go to step 9. Notify observers that the thread is about to sleep. Put the thread to sleep until one of the following events occurs: An event arrives for a port-based input source. A timer fires. The timeout value set for the run loop expires. The run loop is explicitly woken up. Notify observers that the thread just woke up. Process the pending event. If a user-defined timer fired, process the timer event and restart the loop. Go to step 2. If an input source fired, deliver the event. If the run loop was explicitly woken up but has not yet timed out, restart the loop. Go to step 2. Notify observers that the run loop has exited. Example: Detect Main Runloop lag with RunloopObserver 参考资料: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html http://www.tanhao.me/code/151113.html/ 6. Queue Vs Thread Thread != Queue A queue doesn't own a thread and a thread is not bound to a queue. There are threads and there are queues. Whenever a queue wants to run a block, it needs a thread but that won't always be the same thread. It just needs any thread for it (this may be a different one each time) and when it's done running blocks (for the moment), the same thread can now be used by a different queue. There's also no guarantee that a given serial queue will always use the same thread. The only exception is the main queue: dispatch_get_main_queue will must run on main thread. While, main thread may run task at any more than one queue. 7. Dispatch Sync Vs Dispatch Async dispatch_sync dispatch_sync └──dispatch_sync_f └──_dispatch_sync_f2 └──_dispatch_sync_f_slow static void _dispatch_sync_f_slow(dispatch_queue_t dq, void *ctxt, dispatch_function_t func) { _dispatch_thread_semaphore_t sema = _dispatch_get_thread_semaphore(); struct dispatch_sync_slow_s { DISPATCH_CONTINUATION_HEADER(sync_slow); } dss = { .do_vtable = (void*)DISPATCH_OBJ_SYNC_SLOW_BIT, .dc_ctxt = (void*)sema, }; _dispatch_queue_push(dq, (void *)&dss); _dispatch_thread_semaphore_wait(sema); _dispatch_put_thread_semaphore(sema); // ... } Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async,this function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock. Unlike withdispatch_async,no retain is performed on the target queue.Because calls to this function are synchronous, it "borrows" the reference of the caller. Moreover, noBlock_copyis performed on the block. As an optimization, this function invokes the block on the current thread when possible. dispatch_syncdoes two things: queue a block blocks the current thread until the block has finished running dispatch_async dispatch_async(dispatch_queue_t queue, dispatch_block_t block) { dispatch_async_f(dq, _dispatch_Block_copy(work), _dispatch_call_block_and_release); } dispatch_async_f(dispatch_queue_t queue, void *context, dispatch_function_t work); Dead Locks dispatch_sync(queueA, ^{ dispatch_sync(queueB, ^{ dispatch_sync(queueA, ^{ // DEAD LOCK // some task }); }); }); Example: dispatch_async(QueueA, ^{ someFunctionA(...); dispatch_sync(QueueB, ^{ someFunctionB(...); }); }); WhenQueueAruns the block, it will temporarily own a thread, any thread.someFunctionA(...)will execute on that thread. Now while doing the synchronous dispatch,QueueAcannot do anything else, it has to wait for the dispatch to finish.QueueBon the other hand, will also need a thread to run its block and executesomeFunctionB(...). So eitherQueueAtemporarily suspends its thread andQueueBuses some other thread to run the block orQueueAhands its thread over toQueueB(after all it won't need it anyway until the synchronous dispatch has finished) andQueueBdirectly uses the current thread ofQueueA. Needless to say that the last option is much faster as no thread switch is required. Andthisis the optimization the sentence talks about. So adispatch_sync()to a different queue may not always cause a thread switch (different queue, maybe same thread). But adispatch_sync()still cannot happen to the same queue (same thread, yes, same queue, no). That's because a queue will execute block after block and when it currently executes a block, it won't execute another one until this one is done. So it executesBlockAandBlockAdoes adispatch_sync()ofBlockBon the same queue. The queue won't runBlockBas long as it still runsBlockA, but runningBlockAwon't continue untilBlockBhas run. Important:You should never call thedispatch_syncordispatch_sync_ffunction from a task that is executing in the same queue that you are planning to pass to the function. This is particularly important for serial queues, which are guaranteed to deadlock, butshould also be avoided for concurrent queues. 8. Dispatch set target The misunderstanding here is thatdispatch_get_specificdoesn't traverse thestackof nested queues, it traverses thequeue targeting lineage. Modifying the target queue of some objects changes their behavior: Dispatch queues: A dispatch queue's priority is inherited from its target queue. If you submit a block to a serial queue, and the serial queue’s target queue is a different serial queue, that block is not invoked concurrently with blocks submitted to the target queue or to any other queue with that same target queue. Dispatch sources: A dispatch source's target queue specifies where its event handler and cancellation handler blocks are submitted. Dispatch I/O channels: A dispatch I/O channel's target queue specifies where its I/O operations are executed. By default, a newly created queue forwards into the default priority global queue. 参考资料: https://bestswifter.com/deep-gcd/?spm=5176.100239.0.0.vCv2rL https://stackoverflow.com/questions/20860997/dispatch-queue-set-specific-vs-getting-the-current-queue https://stackoverflow.com/questions/23955948/why-did-apple-deprecate-dispatch-get-current-queue https://stackoverflow.com/questions/7346929/why-do-we-use-builtin-expect-when-a-straightforward-way-is-to-use-if-else https://www.objc.io/issues/2-concurrency/concurrency-apis-and-pitfalls/?spm=5176.100239.blogcont17709.5.71pknM libdispatch 源码:https://opensource.apple.com/tarballs/libdispatch/ 9. Read-write Lock in GCD Use dispatch_barrier_async(). When the barrier block reaches the front of a private concurrent queue, it is not executed immediately. Instead, the queue waits until its currently executing blocks finish executing. At that point, the barrier block executes by itself.Any blocks submitted after the barrier block are not executed until the barrier block completes. The queue you specify should be a concurrent queue that you create yourself using thedispatch_queue_create function.If the queue you pass to this function is a serial queue or one of the global concurrent queues, this function behaves like thedispatch_asyncfunction. 附录: 测试Demo:http://files.cnblogs.com/files/smileEvday/iOSMultiThreadSample.zip 部门招人: 高级iOS、Android、前端开发,有意私聊,博主请你喝️ 如果觉得本文帮到了你,记得点赞哦,当然也可以请博主喝一杯豆浆 微信二维码 QQ二维码

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册