你可以使用NSThread 或直接从 NSObject 的类方法 performSelectorInBackground:withObject: 来创建一个线程。
如果你选择thread来实现多线程,那么 NSThread 就是官方推荐优先选用的方式。
一个例子:
//全局变量
SEL mySel;
NSThread *myThread;
int num;
-(void)saySomeThing:(id)sender
{
for(int i=0;i<100;i++)
{
if(![myThread isCancelled])
{
NSLog(@"in thread count : %d",i);
}
if(i==10)
{
[myThread cancel];
}
}
}
mySel =@selector(saySomeThing:);
// [NSThread detachNewThreadSelector:mySel toTarget:self withObject:nil]; //创建并开始,不需要start
myThread=[[NSThread alloc]initWithTarget:self selector:mySel object:nil];//只是创建需要手动start
[myThread start]; //start线程
NSLog(@"out thread... ...");
关于
Parameters
- aSelector
-
The selector for the message to send to the target. This selector must take only one argument and must not have a return value.
aTarget
-
The object that will receive the message aSelector on the new thread.
anArgument
-
The single argument passed to the target. May be
nil
.
关于线程同步
如果我们有多个线程,同时这几个线程中要对统一数据惊醒修改,这是我们就有可能用到线程同步。
-(void)myCount:(id)sender
{
for(int i=0;i<100;i++)
{
num+= [sender integerValue];
NSLog(@"num is %d",num);
}
}
mySel=@selector(myCount:);
[NSThread detachNewThreadSelector:mySel toTarget:self withObject:[NSNumber numberWithInt:1]];
[NSThread detachNewThreadSelector:mySel toTarget:self withObject:[NSNumber numberWithInt:1]];
[NSThread detachNewThreadSelector:mySel toTarget:self withObject:[NSNumber numberWithInt:-1]];
如果你想一个线程对数据修改完后,其他的线程才能修改,这时 ,你会发现这不是你要的结果。
你只需要加上一句
static NSString *mySyn=@"Syn";
-(void)myCount:(id)sender
{
/*
代表这个方法加锁,相当于不管哪一个线程(例如线程A),运行到这个方法时,都要检查有没有其它线程B(或者C、 D等)是否正在用这个方法,有的是的话要等正在使用synchronized方法的线程B(或者C 、D)运行完这个方法后再运行此线程A,没有的话,直接运行。
*/
@synchronized(mySyn)
{
for(int i=0;i<100;i++)
{
num+= [sender integerValue];
NSLog(@"num is %d",num);
}
}
}
关于synchronized
作用:保证此时没有其他线程对self对象进行修改
Using the @synchronized Directive
The @synchronized directive is a convenient way to create mutex locks on the fly in Objective-C code. The @synchronized directive does what any other mutex lock would do—it prevents different threads from acquiring the same lock at the same time. In this case,
however, you do not have to create the mutex or lock object directly. Instead, you simply use any Objective-C object as a lock token, as shown in the following example:
The object passed to the @synchronized directive is a unique identifier used to distinguish the protected block. If you execute the preceding method in two different threads, passing a different object for the anObj parameter on each thread, each would take its lock and continue processing without being blocked by the other. If you pass the same object in both cases, however, one of the threads would acquire the lock first and the other would block until the first thread completed the critical section.
As a precautionary measure, the @synchronized block implicitly adds an exception handler to the protected code. This handler automatically releases the mutex in the event that an exception is thrown. This means that in order to use the @synchronized directive, you must also enable Objective-C exception handling in your code. If you do not want the additional overhead caused by the implicit exception handler, you should consider using the lock classes.
For more information about the @synchronized directive, see The Objective-C Programming Language.