2
3
4
5
6
7
8
9
10
12
13
14
15
16
17
18
19
21
22
23
24
n = IO.read("count.txt") rescue "0"
open("count.txt","w") {|f| f.puts n.to_i + 1}




                       26
n = IO.read("count.txt") rescue "0"                  n = IO.read("count.txt") rescue "0"

open("count.txt","w") {|f| f.puts n.to_i + 1}        open("count.txt","w") {|f| f.puts n.to_i + 1}




                                                27
n = IO.read("count.txt") rescue "0"
open("count.txt","w") {|f| f.puts n.to_i + 1}




                     28
29
30
while (true)
  begin
    break if Dir.mkdir("lock.txt")
  rescue
    sleep(1)
  end
end

n = IO.read("count.txt") rescue "0"
open("count.txt","w") {|f| f.puts n.to_i + 1}

Dir.rmdir("lock.txt")


                        31
lock(“a”)          lock(“b”)
lock(“b”)          lock(“a”)


unlock(“b”)        unlock(“a”)
unlock(“a”)        unlock(“b”)



              32
34
35
36
37
- (void)countThread:(id)param{

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];




   [pool release];
}




   [NSThread detachNewThreadSelector:@selector(countThread:)

   
   
   
   
   
   toTarget:self withObject:self];

   NSLog(@"Thread started.");




                                         38
39
40
- (int) maxOfArray:(int *)a count:(int)n {
  static int max = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > max) max = a[i];
  }
  return max;
}




                         41
- (int)randomNumber {
  static int rand = 7654321;
  rand = (rand * 12345 + 678901) & 0x7fff;
  return rand;
}




                         42
43
NSMutableDictionary *threadLoacl =
   [[NSTread currentThread] threadDictonary];

[threadLocal setObject:@”Test” forKey:@”mode”];


s = [threadLocal objectForKey:@”mode”]




                        44
@interface MyInt : NSObject {
  int intValue;
}
@end
@implementation MyInt
-(int) getInt {
  return intValue;
}
-(void) setOddInt:(int)i {
  intValue = i;
  intValue = (intValue >> 1) << 1;
}
@end


                        45
@interface MyString : NSObject {
  NSMutableString *str;
}
@end
@implementation MyInt
-(MyString *) getString {
  return str;
}
-(void) addString:(NSString *)s {
  [str appendString:s];
}
@end



                        46
47
48
@interface MyPoint : NSObject {
  GCPoint point;
}
@end
@implementation MyInt
-(GCPoint) getPoint {
  return point;
}
-(void) setPoint:(GCPoint)p {
  point = p;
}
@end



                        49
@interface MyString : NSObject {
  NSString *string;
}
@property (nonatomic, retain) NSString *string;
@end

@implementation MyString
@synthesize string;
@end




                           50
51
52
54
55
57
58
59
@interface CountOperation : NSOperation {

   id delegate;
}


@implementation CountOperation

- (id)initWithDelegate:(id)aDelegate {

    self = [super init];

    if (self != nil) {

    
       delegate = aDelegate;

    }

    return self;
}

- (void)main {

   NSAutoreleasePool *pool = [NSAutoreleasePool new];


  

  [pool release];
}
@end



   queue = [[NSOperationQueue alloc] init];



   CountOperation *op = [[[CountOperation alloc] initWithDelegate:self] autorelease];

   [queue addOperation:op];

   NSLog(@"Thread started.");
}


                                                     60
62
63
{|ary|           ^(NSArray *ary) {
  s = “”           NSMutableString *s = [NSMutableString string];
  for a in ary     for (NSString *a in ary) {
    s += a           [s appendString:a];
  end              }
  s                return s;
}                }



                               64
__block NSMutableString *s = [NSMutableString string];
^(NSArray *ary) {
  for (NSString *a in ary) {
    [s appendString:a];
  }
}




                            65
- (NSArray *)sortedArrayUsingSelector:
(SEL)comparator

- (NSArray *)sortedArrayUsingComparator:
(NSComparator)cmptr




                66
67
68
70
72

Grand centraldispatch

  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 21.
  • 22.
  • 23.
  • 24.
  • 26.
    n = IO.read("count.txt")rescue "0" open("count.txt","w") {|f| f.puts n.to_i + 1} 26
  • 27.
    n = IO.read("count.txt")rescue "0" n = IO.read("count.txt") rescue "0" open("count.txt","w") {|f| f.puts n.to_i + 1} open("count.txt","w") {|f| f.puts n.to_i + 1} 27
  • 28.
    n = IO.read("count.txt")rescue "0" open("count.txt","w") {|f| f.puts n.to_i + 1} 28
  • 29.
  • 30.
  • 31.
    while (true) begin break if Dir.mkdir("lock.txt") rescue sleep(1) end end n = IO.read("count.txt") rescue "0" open("count.txt","w") {|f| f.puts n.to_i + 1} Dir.rmdir("lock.txt") 31
  • 32.
    lock(“a”) lock(“b”) lock(“b”) lock(“a”) unlock(“b”) unlock(“a”) unlock(“a”) unlock(“b”) 32
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
    - (void)countThread:(id)param{ NSAutoreleasePool*pool = [[NSAutoreleasePool alloc] init]; [pool release]; } [NSThread detachNewThreadSelector:@selector(countThread:) toTarget:self withObject:self]; NSLog(@"Thread started."); 38
  • 39.
  • 40.
  • 41.
    - (int) maxOfArray:(int*)a count:(int)n { static int max = 0; for (int i = 0; i < n; i++) { if (a[i] > max) max = a[i]; } return max; } 41
  • 42.
    - (int)randomNumber { static int rand = 7654321; rand = (rand * 12345 + 678901) & 0x7fff; return rand; } 42
  • 43.
  • 44.
    NSMutableDictionary *threadLoacl = [[NSTread currentThread] threadDictonary]; [threadLocal setObject:@”Test” forKey:@”mode”]; s = [threadLocal objectForKey:@”mode”] 44
  • 45.
    @interface MyInt :NSObject { int intValue; } @end @implementation MyInt -(int) getInt { return intValue; } -(void) setOddInt:(int)i { intValue = i; intValue = (intValue >> 1) << 1; } @end 45
  • 46.
    @interface MyString :NSObject { NSMutableString *str; } @end @implementation MyInt -(MyString *) getString { return str; } -(void) addString:(NSString *)s { [str appendString:s]; } @end 46
  • 47.
  • 48.
  • 49.
    @interface MyPoint :NSObject { GCPoint point; } @end @implementation MyInt -(GCPoint) getPoint { return point; } -(void) setPoint:(GCPoint)p { point = p; } @end 49
  • 50.
    @interface MyString :NSObject { NSString *string; } @property (nonatomic, retain) NSString *string; @end @implementation MyString @synthesize string; @end 50
  • 51.
  • 52.
  • 54.
  • 55.
  • 57.
  • 58.
  • 59.
  • 60.
    @interface CountOperation :NSOperation { id delegate; } @implementation CountOperation - (id)initWithDelegate:(id)aDelegate { self = [super init]; if (self != nil) { delegate = aDelegate; } return self; } - (void)main { NSAutoreleasePool *pool = [NSAutoreleasePool new]; [pool release]; } @end queue = [[NSOperationQueue alloc] init]; CountOperation *op = [[[CountOperation alloc] initWithDelegate:self] autorelease]; [queue addOperation:op]; NSLog(@"Thread started."); } 60
  • 62.
  • 63.
  • 64.
    {|ary| ^(NSArray *ary) { s = “” NSMutableString *s = [NSMutableString string]; for a in ary for (NSString *a in ary) { s += a [s appendString:a]; end } s return s; } } 64
  • 65.
    __block NSMutableString *s= [NSMutableString string]; ^(NSArray *ary) { for (NSString *a in ary) { [s appendString:a]; } } 65
  • 66.
    - (NSArray *)sortedArrayUsingSelector: (SEL)comparator -(NSArray *)sortedArrayUsingComparator: (NSComparator)cmptr 66
  • 67.
  • 68.
  • 70.
  • 72.