Objective-C 编程语言官网文档(十二)-线程

Objective-C 提供了对线程同步以及异常绑定( “Exception Handling.” )的支持。要打开对这些特性的支持。可以使用GCC3.3以及更新版本 -fobjc-exceptions 开关。

Objective-C 支持多线程。因此,两个线程可以在同时去改变同一个对象,这时就可能在程序中产生问题。为了避免一段代码在运行时被多个线程操作, Objective-C 提供了 @synchronized() 指令.

@synchronized()指令会为一段代码加锁以在同一时间确保只有一个线程可以使用。其它的线程被锁定直到当前获得锁的线程退出受保护的代码。

@synchronized() 指令只有一个参数,可以是任何 Objective-C 对象, 包括 self. 这个对象被称为互斥信号量. 它允许一个线程锁定一个代码块以防止同一时间被其它线程访问。你应该使用不同的信号来保护程序的不同的临界区。最安全的方法就是在产生多线程的状况之前创建好所有需要的互斥信号对象,以防止出现紊乱状况。

11-1 展示了使用 self 来作为当前对象的实例方法同步访问的信号量。与此类似,你可以使用类对象为相关类的类方法进行同步保护。在后面一种情况中,同一时间只会有一个线程可以执行这个类方法,因为对于所有的调用者来说,都只有一个类对象可被使用。

11-1

11-2 展示了一个更常用的方式。在执行一个关键流程时,代码从 Account 类中获得了一个信号量并用它来锁定该关键流程快。 Account 类可以在它的 initialize 方法中创建该信号量.

11-2

Objective-C 的同步特性支持可迭代和可重入代码。一个线程可以在一个可迭代情况下多次使用单个信号量。其它的线程则被锁住直到占有锁的线程释放锁,即当 @synchronized() 块正常退出或者通过异常退出后。

当在 @synchronized() 块中的代码抛出一个异常时, Objective-C 运行时会捕获这个异常,释放该信号量 (这样被保护的代码即可以被其它线程执行), 并再次抛出该异常给下一个异常处理器。

英文原文:点击打开链接

Objective-C provides support for thread synchronization and exception handling, which are explained in this chapter and in “Exception Handling.” To turn on support for these features, use the -fobjc-exceptions switch of the GNU Compiler Collection (GCC) version 3.3 and later.

Objective-C supports multithreading in applications. Therefore, two threads can try to modify the same object at the same time, a situation that can cause serious problems in a program. To protect sections of code from being executed by more than one thread at a time, Objective-C provides the @synchronized() directive.

The @synchronized()directive locks a section of code for use by a single thread. Other threads are blocked until the thread exits the protected code—that is, when execution continues past the last statement in the @synchronized() block.

The @synchronized() directive takes as its only argument any Objective-C object, including self. This object is known as a mutual exclusion semaphore or mutex. It allows a thread to lock a section of code to prevent its use by other threads. You should use separate semaphores to protect different critical sections of a program. It’s safest to create all the mutual exclusion objects before the application becomes multithreaded, to avoid race conditions.

Listing 11-1 shows code that uses self as the mutex to synchronize access to the instance methods of the current object. You can take a similar approach to synchronize the class methods of the associated class, using the class object instead of self. In the latter case, of course, only one thread at a time is allowed to execute a class method because there is only one class object that is shared by all callers.

Listing 11-1 Locking a method using self

Listing 11-2 shows a general approach. Before executing a critical process, the code obtains a semaphore from the Account class and uses it to lock the critical section. The Account class could create the semaphore in its initialize method.

Listing 11-2 Locking a method using a custom semaphore

The Objective-C synchronization feature supports recursive and reentrant code. A thread can use a single semaphore several times in a recursive manner; other threads are blocked from using it until the thread releases all the locks obtained with it; that is, every @synchronized() block is exited normally or through an exception.

When code in an @synchronized() block throws an exception, the Objective-C runtime catches the exception, releases the semaphore (so that the protected code can be executed by other threads), and rethrows the exception to the next exception handler.

Original: https://www.cnblogs.com/andriod-html5/archive/2012/06/06/2539323.html
Author: andriod2012
Title: Objective-C 编程语言官网文档(十二)-线程

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/540137/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • junit结合spring-test里的MockMvc来测试SpringMvc接口方法

    如下是SpringMvc项目里的Controller 重点来了,junit结合spring-test里的MockMvc来测试上面的http接口 运行结果(支持debug调试,是不是…

    Java 2023年5月29日
    0159
  • CAS 单点登录【2】自定义用户验证

    方案1:CAS默认的JDBC扩展方案: CAS自带了两种简单的通过JDBC方式验证用户的处理器。 这两个处理类位于cas-server-support-jdbc这个扩展工程下。 第…

    Java 2023年5月29日
    081
  • 回形数格式方阵

    package com.atguigu.exer; import java.util.Scanner; public class Huixing{public static voi…

    Java 2023年6月13日
    086
  • JAVA:聊聊JAVA基础(2)- 基本语法(待续)

    Java中的注释 单行注释 //这是一个单行&a…

    Java 2023年6月5日
    084
  • Java Servlet单元测试

    Java Servlet单元测试 1. 解决痛点 虽然目前主流的开发方式,很多都是通过controll或者微服务提供api.但是不免还是需要写几个 servlet完成接口开发.按照…

    Java 2023年6月13日
    092
  • Lamda(拉姆达)表达式演化过程

    HI!小伙伴们,好久没见了,4月份开始断更,中途有点事儿,今天开始更新了,整理一篇Lamda表达式演化过程,希望喜欢的一如既往的支持! 传统集合过滤 现在有2个需求:在一组学生集合…

    Java 2023年6月8日
    073
  • 推荐一款M1芯片电脑快速搭建集群的虚拟机软件

    虚拟机软件太多了,出名的莫过于 VMware, VirutlaBox以及 Parallels Desktop。 我们使用虚拟机软件一般有两种用途: 安装不同于宿主机系统的拥有用户界…

    Java 2023年6月7日
    0106
  • 从new File(“”)到jdk源码

    概述 今天在项目中看到下面两行代码,看注释说是获取当前工作路径,之前也没有用过这种用法,比较好奇还能这样用,所以研究了一下源码。 //获&#x53D6…

    Java 2023年6月14日
    079
  • 限流常见方案

    限流常见方案 我歌月徘徊,我舞影零乱。醒时相交欢,醉后各分散。 一、限流思路 常见的系统服务限流模式有:熔断、服务降级、延迟处理和特殊处理四种。 1、熔断 将熔断措施嵌入到系统设计…

    Java 2023年6月5日
    074
  • 开发必备之单元测试

    祸乱生于疏忽 单元测试先于交付。穿越暂时黑暗的时光隧道,才能迎来系统的曙光。 单元测试的相关介绍 ​ 计算机世界里的软件产品通常是由模块组合而成的 模块又可以分成诸多子模块。 比如…

    Java 2023年6月5日
    074
  • mysql多选查询,存为以逗号隔开的字符串,查询回显也为以逗号隔开的标题

    1.需求: 数据库储存格式 需要展示为 SELECTu.*,( SELECT GROUP_CONCAT( NAME SEPARATOR ‘,’ ) FROM…

    Java 2023年6月14日
    079
  • springboot 注解

    1、@SpringBootApplication Spring Boot 最最最核心的注解,用在 Spring Boot 主类上,标识这是一个 Spring Boot 应用,用来开…

    Java 2023年5月30日
    066
  • Java基础 awt Font 四种字体样式

    JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code packag…

    Java 2023年5月29日
    0127
  • mysql和redis数据最终一致性的解决方案

    此问题是无法做到100%场景一致性的,只能做到基本一致或者最终一致性。 推荐使用的方案 延时双删 原理:先进行缓存清除,再执行update,最后(延迟N秒)再执行缓存清除。(延迟N…

    Java 2023年6月8日
    079
  • Ajax结合THymeleaf使用

    <script type="text/javascript"> //通过$("标签…

    Java 2023年6月5日
    093
  • Java 插入公式到PPT幻灯片

    PowerPoint幻灯片中可插入公式,用于在幻灯片放映时演示相关内容的论证、推算的依据,能有效地为演讲者提供论述的数据支撑。通过后端程序代码,我们可借助特定的工具来实现在幻灯片中…

    Java 2023年5月29日
    081
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球