달력

10

« 2024/10 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2010. 5. 28. 21:32

[16일차]에러처리 Objective-C2010. 5. 28. 21:32

1. 에러 처리

에러가 발생하면 메서드나 함수의 리턴 값 형태로 에러의 발생을 리턴합니다.

파일 처리에는 이러한 에러 처리를 위한 객체를 대입하도록 되어 있습니다.

이 경우 에러 처리에 관련된 오브젝트를 대입하면 에러가 발생해도 프로그램이 중단되지 않고 적절한 메시지를 출력하도록 프로그램을 만들 수 있습니다.


2. 타이머

일정한 주기를 가지고 또는 일정 시간을 delay 한 후 실행해야 하는 프로그램이나 메서드가 있다면 이 때는 타이머 오브젝트를 이용할 수 있습니다.

이 클래스는 NSTimer.h에 정의되어 있습니다.



3. 스레드(Thread)

NSThread 클래스가 스레드 기능을 제공하며 NSThread.h 파일에 정의와 구현되어 있습니다.

실제 main 함수도 스레드로 동작합니다.
모든 Objective-c프로그램 1개 이상의 스레드 소유.
기본적으로 main도 하나의 main 스레드로 생성해서 실행

현재 스레드가 수행 중인지 아닌지 확인하는 메서드는 아래와 같습니다.

 

+(BOOL)isMultiThreaded

멀티 스레드인지 아닌 지 확인해주는 메서드. 과거 한번이라도 수행이 되었는지 확인

+(NSThread *)currentThread

현재 스레드를 나타내는 인스턴스 리턴

+(NSThread *)mainThread

현재의 main 스레드를 나타내는 인스턴스 리턴
---------------------------------------------------------------
#import <Foundation/Foundation.h>
int main()
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    NSThread * thread;
    thread = [NSThread currentThread];
    if(thread !=nil)
     NSLog(@"현재 스레드가 동작중입니다");
    else
     NSLog(@"현재 스레드가 동작중이지 않습니다.");
    
    if(thread == [NSThread mainThread])
     NSLog(@"이 스레드는 main스레드 입니다.");
    else
     NSLog(@"이 스레드는 main스레드가 아닙니다");
    
    if([NSThread isMultiThreaded] == YES)
     NSLog(@"현재 프로그램은 멀티 스레드 환경입니다.");
    else
     NSLog(@"현재 프로그램은 멀티 스레드 환경이 아닙니다.");        
    
    [pool drain];
    system("pause");
    return 0;
}

---------------------------------------------------------------

스레드 생성하기

-(id) initWithTarget:(id)스레드 수행 주체 selector:@selector(스레드로 수행할 메서드:) object:인수로 넘겨줄 오브젝트-스레드함수에게 넘겨줄 매개변수];

 

스레드 수행 메서드

-(void)메서드명:(id)argument

{        

    작업 내용

}

-스레드 시작
[스레드객체 Start];

-스레드 중지
+(void)exit // 현재 실행중인 스레드 중지
-(void)cancel // 호출하는 인스턴스 스레드가 중지

ex)스레드로 메서드 수행
---------------------------------------------------------------
#import <Foundation/Foundation.h>
#import <stdlib.h>
@interface ThreadTest:NSObject
{
    NSThread *thread;
}     
-(void)setThread; // 스레드 생성하고 시작
-(void)Print:(id)argument; // 스레드가 수행할 메서드
@end

@implementation ThreadTest
-(void)setThread
{
//  -(id) initWithTarget:(id)스레드 수행 주체 selector:@selector(스레드로 수행할 메서드:) object:인수로 넘겨줄 오브젝트-스레드함수에게 넘겨줄 매개변수];
    thread = [[NSThread alloc] initWithTarget:self selector:@selector(Print:) object:nil];
               // 생성                                시작
    [thread start];
}
-(void)Print:(id)argument
// 비선점
{        
    NSLog(@"Hello World");               
}
@end

int main()
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    ThreadTest *Obj = [[ThreadTest alloc]init]; // 현재 스레드 생성 안됨
    if([NSThread isMultiThreaded] == YES)
     NSLog(@"현재 프로그램은 멀티 스레드 환경입니다.");
    else
     NSLog(@"현재 프로그램은 멀티 스레드 환경이 아닙니다.");
     [Obj setThread];
     if([NSThread isMultiThreaded] == YES)
     NSLog(@"현재 프로그램은 멀티 스레드 환경입니다.");
     else
     NSLog(@"현재 프로그램은 멀티 스레드 환경이 아닙니다.");
    [pool drain];   
    system("pause");    
    return 0;
}
---------------------------------------------------------------

//* 두개의 스레드 수행
---------------------------------------------------------------
#import <Foundation/Foundation.h>
#import <stdlib.h>
@interface ThreadTest:NSObject
{
    NSThread *thread1;
    NSThread *thread2;
}     
-(void)setThread;
-(void)Print:(id)argument;
@end

@implementation ThreadTest
-(void)setThread
{
    thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(Print:) object:nil];
    thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(Disp:) object:nil];
    [thread1 start];
    [thread2 start];
}
-(void)Print:(id)argument
{
         int i;                         
         for(i=0; i<10; i++)                
         {
                  NSLog(@"Hello World");
                  Sleep(1000);
         } 
         NSLog(@"Print 메서드 수행 종료");     
}
-(void)Disp:(id)argument
{
         int i;                         
         for(i=0; i<10; i++)                
         {
                  NSLog(@"Hi Objective-C");
                  Sleep(1000);                 
         } 
          NSLog(@"Disp 메서드 수행 종료");      
}
@end

int main()
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    ThreadTest *Obj = [[ThreadTest alloc]init];
    [Obj setThread];
    [pool drain];
    system("pause");
    return 0;
}
---------------------------------------------------------------

멀티스레드의 자원공유문제에러 => Mutex 에러 // Syncronize 하지 않아서 발생
                              // (Mutual Exclusion - 상호배제에러)

#import <Foundation/Foundation.h>
#import <stdlib.h>
@interface ThreadTest:NSObject
{
    NSThread *thread1;
    NSThread *thread2;
    int share;
}     
-(void)setThread;
-(void)Print:(id)argument;
@end

@implementation ThreadTest
-(void)setThread
{
    thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(Print:) object:nil];
    thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(Disp:) object:nil];
    [thread1 start];
    [thread2 start];
    share = 0;
}
-(void)Print:(id)argument
{
         int i;                                        
          for(i=0; i<10; i++)                
          {                
                  share --;
                  Sleep(100);
                  NSLog(@"감소 시킨 share 값: %i:", share);
          }       
}
-(void)Disp:(id)argument
{
         int i;
       
         for(i=0; i<10; i++)                
         {
                  share ++;
                  Sleep(100);
                  NSLog(@"증가 시킨 share 값: %i:", share);
         }         
}
@end

int main()
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    ThreadTest *Obj = [[ThreadTest alloc]init];
    [Obj setThread];
    [pool drain];
    system("pause");
    return 0;
}
---------------------------------------------------------------

:
Posted by 에너지발전소