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/89122/

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

(0)

大家都在看

  • java通过command调用openssl生成私钥和证书

    在windows环境下进行的测试,前提条件,windows上需要先安装openssl。 配置环境变量,查看版本: import java.io.*; import java.uti…

    Java 2022年9月21日
    0120
  • SpringBoot项目使用Swagger2(丝袜哥)实现接口测试管理

    Original: cannot be found on object of type ‘org.springframework.cache.interceptor.C…

    Java 2022年9月22日
    0101
  • Java八大基本数据类与对应包装类

    Java的八种基本数据类型 序号 数据类型 字节数 默认值 取值范围 示例 1 byte (位) 1 0 -2^7 ~ 2^7-1 byte b = 10; 2 short (短整…

    Java 2022年10月12日
    097
  • Drools规则引擎实践直白总结

    Drools规则引擎,网上大把相关的文章介绍,但我感觉不够直白,理解有些困难,且知识点没有集中比较分散、有些还是早前版本的内容,对与新手来说上手可能比较慢,而且比较容易走弯路,故我…

    Java 2022年10月27日
    0115
  • Java8日期常用方法

    java;gutter:true; import junit.framework.TestCase;</p> <p>import java.time.*; …

    Java 2022年9月21日
    0198
  • Java maven构建命令使用总结

    实践环境 Apache Maven 3.0.5 (Red Hat 3.0.5-17) maven构建生命周期 学习Maven构建命令之前,我们不烦先简单了解下Maven构建生命周期…

    Java 2022年11月12日
    079
  • Springboot Maven 动态加载配置文件profile

    1.maven 默认没有profile 需要在pom中增加配置profile配置 <id>devid> true dev dev <id>testid…

    Java 2022年9月22日
    095
  • 以Integer类型传参值不变来理解Java值传参

    最近在写代码的时候出了一个错误,由于对值引用理解的不深,将Integer传入方法中修改,以为传入后直接修改Integer中的值就不用写返回值接收了,虽然很快发现了问题,但还是来总结…

    Java 2022年9月21日
    098
  • Nginx核心知识100讲学习笔记(陶辉)详解HTTP模块(详解11阶段)

    一、server_name指令 1、指令后可跟多个域名。第一个是主域名 bash;gutter:true; Syntax server_name_in_redirect on | …

    Java 2022年9月23日
    0136
  • ssh 登陆远程nohup java 脚本无效,但设置生效后日志中文乱码

    该日志说到了两个问题,分别解答: (1)ssh 登陆远程调用含有nohup java的启动脚本失败 (2)ssh 登陆远程调用启动脚本生效后,但是日志中出现中文乱码 场景:jenk…

    Java 2022年9月21日
    095
  • Java获取本机IP

    import lombok.extern.slf4j.Slf4j; import org.junit.Test; import java.net.Inet4Address; imp…

    Java 2022年9月21日
    0128
  • java重试

    重试 重试,就是多试几次。一次不成功,多试几次说不定就成功了。 什么时候重试? 要执行的逻辑比较重要,或者是服务不稳定,或者是Rpc远程调服务有时不成功,都可以使用重试。 示例代码…

    Java 2022年9月21日
    062
  • JAVA-JDBC

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com Original: …

    Java 2022年9月21日
    094
  • java 代码的良好习惯

    有很多书籍提到了代码开发的良好习惯,但是自己看过后,在开发中并不能每次都想起来。在此处开贴做笔记,以后自己开发的代码,必须符合。 [En] There are many books…

    Java 2022年9月21日
    091
  • 力扣算法笔记–3–旋转图像(数组)

    ·旋转图像 给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。 示例: 分析: 在另一个数组的帮助下,原始矩阵的所有元素都存储在该数组中…

    Java 2022年11月10日
    077
  • harbor安装

    Harbor 简介 Docker容器应用的开发和运行离不开可靠的镜像管理,虽然Docker官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的Registr…

    Java 2022年11月10日
    0115
最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总