NSMutableArray线程安全(NSMutableArray thread safety)
在我的应用程序中,我正在从多个线程访问和更改可变数组。 在开始时,当我尝试访问
objectAtIndex
对象时发生崩溃,因为索引超出了范围(该索引处的对象已经从另一个线程中的数组中移除)。 我在互联网上搜索如何解决这个问题,我决定尝试这个解决方案 。我做了一个NSMutableArray
属性的类,请参阅以下代码:@interface SynchronizedArray() @property (retain, atomic) NSMutableArray *array; @end @implementation SynchronizedArray - (id)init { self = [super init]; if (self) { _array = [[NSMutableArray alloc] init]; } return self; } -(id)objectAtIndex:(NSUInteger)index { @synchronized(_array) { return [_array objectAtIndex:index]; } } -(void)removeObject:(id)object { @synchronized(_array) { [_array removeObject:object]; } } -(void)removeObjectAtIndex:(NSUInteger)index { @synchronized(_array) { [_array removeObjectAtIndex:index]; } } -(void)addObject:(id)object { @synchronized(_array) { [_array addObject:object]; } } - (NSUInteger)count { @synchronized(_array) { return [_array count]; } } -(void)removeAllObjects { @synchronized(_array) { [_array removeAllObjects]; } } -(id)copy { @synchronized(_array) { return [_array copy]; } }
我使用这个类而不是旧的可变数组,但该应用程序仍在这条线上崩溃:
return [_array objectAtIndex:index];
我也试过这种方法与NSLock
,但没有运气。 我做错了什么以及如何解决这个问题?In my app I'm accessing and changing a mutable array from multiple threads. At the beginning it was crashing when I was trying to access an object with
objectAtIndex
, because index was out of bounds (object at that index has already been removed from array in another thread). I searched on the internet how to solve this problem, and I decided to try this solution .I made a class withNSMutableArray
property, see the following code:@interface SynchronizedArray() @property (retain, atomic) NSMutableArray *array; @end @implementation SynchronizedArray - (id)init { self = [super init]; if (self) { _array = [[NSMutableArray alloc] init]; } return self; } -(id)objectAtIndex:(NSUInteger)index { @synchronized(_array) { return [_array objectAtIndex:index]; } } -(void)removeObject:(id)object { @synchronized(_array) { [_array removeObject:object]; } } -(void)removeObjectAtIndex:(NSUInteger)index { @synchronized(_array) { [_array removeObjectAtIndex:index]; } } -(void)addObject:(id)object { @synchronized(_array) { [_array addObject:object]; } } - (NSUInteger)count { @synchronized(_array) { return [_array count]; } } -(void)removeAllObjects { @synchronized(_array) { [_array removeAllObjects]; } } -(id)copy { @synchronized(_array) { return [_array copy]; } }
and I use this class instead of old mutable array, but the app is still crashing at this line:
return [_array objectAtIndex:index];
I tried also this approach withNSLock
, but without a luck. What I'm doing wrong and how to fix this?
原文:https://stackoverflow.com/questions/21139581
满意答案
你已经快到了。 大概。
您正在使用正确的方法来检索注释,因此您可以使用值。
您的问题 - 如果我解释非常简约的问题描述 (!),您只通过代码片段(!)中的注释正确提供 - 是(错误的)假设将类型Class的数组粘贴到System.out.println中( )将打印出它包含的类的名称。 它不是。 而是打印有关参考的信息:
[Ljava.lang.Class;@15db9742
如果需要类的名称,则必须遍历该数组的元素并使用.getName(),. getSimpleName()或提供Class方法的其他名称之一。
有关如何打印数组元素的更多信息,请访问:
当然,如果问题在于您从注释字段中获取空值,那么整个答案可能完全不同于这一点。 但是,由于您没有提供足够的问题描述(“ 无效 ” 不是问题描述! ),我们只能猜测您的问题是什么。
The solution for this is making sure the advice method's parameter name match the parameter name in AspectJ expression. In my case, the advice method should look like this:
@Aspect public class MyAspect { @Around("@annotation(myAnnotation)") public Object handle(ProceedingJoinPoint joinPoint, MyAnnotation myAnnotation) { System.out.println(myAnnotation.exceptionList); // should print out TimeOutException } }
相关问答
更多IntelliJ IDEA + AspectJ(IntelliJ IDEA + AspectJ)
使用Spring或AspectJ将基于代码的样式转换为基于注释的样式AOP(Converting Code based style to Annotation Based style AOP using Spring or AspectJ)
使用AspectJ Annotation获取方法输入的属性(Get Method Input's Attributes using AspectJ Annotation)
aspectj切入点匹配特定的注释参数值(aspectj pointcut matching a specific annotation parameter value)
使用Scala中的aspectj修改方法参数值(Modify a method parameter value using aspectj in Scala)
使用AspectJ获取注释参数(Get Annotation Parameter with AspectJ)
使用AspectJ的Spring / @Transactional完全被忽略(Spring / @Transactional with AspectJ is totally ignored)
AspectJ:如何替换现有注释(AspectJ: How to replace an existing annotation)
如何将基于注释的转换为本机AspectJ语法(How to convert annotation-based into native AspectJ syntax)
aspectj中注释的优先顺序(precedence order for annotations in aspectj)
相关文章
更多线程报错 thread
引入thread后socket接受不了报文了
关于Thread类中的start()方法和run()方法
一步一步掌握java的线程机制(二)----Thread的生命周期
org.apache.zookeeper.server.NIOServerCnxn - Thread Thread[main,5,main] died java.lang.NoSuchFieldError: AuthFailed
关于线程
action 线程问题。
为什么执行JAVA程序时,会出现Exception in thread"main" java.lan
多线程问题
java线程状态详解(6种)
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!