RunLoop配置实践:线程中的高效管理

0 下载量 116 浏览量 更新于2024-09-01 收藏 287KB PDF 举报
"《Threading Programming Guide》笔记3:RunLoop操作配置实践" 本文是对苹果官方文档《Threading Programming Guide》的深入解读,聚焦于RunLoop在实际编程中的操作和配置。作者付宇轩在系列笔记的第三部分,分享了如何在自创线程中创建、配置和管理RunLoop。 在iOS和macOS开发中,RunLoop是线程管理的核心组件,特别是在主线程中,它是随应用程序启动而自动创建并运行的。由于现代Xcode项目模板会默认处理主线程的RunLoop,开发者通常无需关注其状态。然而,在自定义的子线程中,开发者则需要手动创建和配置RunLoop,以便更好地控制线程的行为和资源消耗。 RunLoop的主要功能是保持线程活跃但不消耗过多资源。是否需要在子线程中使用RunLoop,取决于线程的任务性质。一次性任务或长时间运行的任务可能不需要RunLoop,而周期性任务、需要频繁与主线程交互的任务,或者利用Cocoa框架的`performSelector…`系列方法时,则强烈推荐使用RunLoop。使用RunLoop的典型场景包括: 1. 基于端口或自定义数据源与其他线程通信。 2. 在线程上执行定时任务。 3. 使用Cocoa框架的定时器功能。 4. 执行周期性或频繁的任务。 为了操作RunLoop,我们需要首先获取到RunLoop对象。在Cocoa层,我们可以使用`NSRunLoop`类,而在CoreFoundation层,我们则需要操作`CFRunLoopRef`指针。尽管两种框架都没有提供直接创建RunLoop的方法,但我们可以通过以下方式获取线程的RunLoop: - `NSRunLoop`: 使用`currentRunLoop`方法获取当前线程的RunLoop。 - `CFRunLoopRef`: 调用`CFRunLoopGetCurrent()`函数获取当前线程的RunLoop引用。 一旦获取到RunLoop对象,就可以添加输入源(Inputsources)、定时器(Timers)和观察者(Observers)来定制RunLoop的行为。例如,输入源可以响应特定事件,定时器用于定期触发某些操作,而观察者则可以在RunLoop状态改变时执行回调。 配置RunLoop涉及到设置运行模式(Run Modes),不同的运行模式决定了哪些输入源和定时器会被激活。`NSRunLoop`提供了`commonModes`属性,包含了常用模式,如`NSDefaultRunLoopMode`,可以确保在大多数情况下,RunLoop都能正常工作。 理解并合理使用RunLoop对于优化iOS和macOS应用程序的性能和资源管理至关重要。开发者需要根据应用的需求,精确地创建、配置和调度RunLoop,以实现高效、低耗的多线程编程。通过深入学习《Threading Programming Guide》以及实践,可以进一步提升这方面的能力。

Exception in Application constructor Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:893) Caused by: java.lang.RuntimeException: Unable to construct Application instance: class Main at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:750) Caused by: java.lang.NoSuchMethodException: Main.<init>() at java.lang.Class.getConstructor0(Class.java:3110) at java.lang.Class.getConstructor(Class.java:1853) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$7(LauncherImpl.java:818) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177) ... 1 more出现这个报错怎么解决

2023-05-26 上传

Exception in Application constructor Exception in thread "main" java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071) Caused by: java.lang.RuntimeException: Unable to construct Application instance: class com.example.demo.Main at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:891) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class com.example.demo.Main (in module com.example.javafxtest) because module com.example.javafxtest does not export com.example.demo to module javafx.graphics at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392) at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:489) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:803) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456) at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184) ... 1 more

2023-05-24 上传
2023-07-14 上传