我正在做这个多线程应用程序只是为了看看@synchronized 指令是如何工作的。我读过如果所有线程都具有与@synchronized 的参数相同的对象,那么它们都在同一个锁上等待。所以我想使用 self作为参数,因为它对于所有线程都是相同的。
在这个应用程序中,有一个文本字段被所有线程多次编辑。我不关心性能,这只是一个测试,所以我没有将@synchronized 指令放在 for 之前,而是放在里面。
我使用的属性:
@property (weak) IBOutlet NSTextField *textField;
@property (nonatomic, copy) NSNumber* value;
@property (nonatomic,copy) NSMutableArray* threads;
编码:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
value= @0;
textField.objectValue= value;
for(int i=0; i<10; i++)
{
NSThread* thread=[[NSThread alloc]initWithTarget: self selector: @selector(routine:) object: nil];
[threads addObject: thread];
[thread start];
}
}
- (void) routine : (id) argument
{
for(NSUInteger i=0; i<100; i++)
{
@synchronized(self)
{
value= @(value.intValue+1);
textField.objectValue= value;
}
}
}
有时应用程序成功,我看到 1000 作为文本字段值。但有时不是,我担心这是饥饿,我在文本字段上看不到任何内容,它是空的。我尝试了调试,但很难看到是什么错了,因为失败的标准对我来说似乎很随意,有时它可以正常工作。
解决方案
@synthesize threads,value, textField;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
value= @0;
threads=[[NSMutableArray alloc]initWithCapacity: 100];
for(NSUInteger i=0; i<100; i++)
{
NSThread* thread=[[NSThread alloc]initWithTarget: self selector: @selector(routine:) object: nil ];
[threads addObject: thread];
[thread start];
}
}
- (void) routine : (id) arg
{
for(NSUInteger i=0; i<1000; i++)
{
@synchronized(self)
{
value= @(value.intValue+1);
[textField performSelectorOnMainThread: @selector(setObjectValue:) withObject: value waitUntilDone: NO];
}
}
}