Use multiple threads of the iphone to implement the "ticket sales system" (hands-on Guide to iphone development-basics)

Source: Internet
Author: User

Author: Sun Dongfeng)

 

Java provides a lot of convenience for programmers because it supports multiple threads. In fact, it also supports multi-thread programming in iphone development, and it is no more troublesome than java.

In this article, I will take the classic "multi-thread Ticket System" in most Java tutorials as an example to implement the same in the iphone.

 

The following is the java version of the "multi-thread Ticket System" code:

Package demo;

Public class SellTickets implements Runnable {

Private int tickets = 100;

Public void run (){

Int count = 0;

While (true)

{

// Lock

Synchronized (this ){

If (tickets> 0 ){

Try {

Thread. sleep (500 );

} Catch (InterruptedException e ){

E. printStackTrace ();

}

Count = 100-tickets;

System. out. println ("the current number of votes is:" + tickets + "sold" + count

+ "Thread name:" + Thread. currentThread (). getName ());

Tickets --;

} Else {

Break;

}

}

}

}

Public static void main (String [] args ){

SellTickets r = new SellTickets ();

Thread t1 = new Thread (r, "t1 ");

T1.start ();

Thread t2 = new Thread (r, "t2 ");

T2.start ();

Thread t3 = new Thread (r, "t3 ");

T3.start ();

Thread t4 = new Thread (r, "t4 ");

T4.start ();

}

}

 

After the code of the above java version is executed, the console output is as follows:

Current ticket count: 100 sold 0 thread name: t1

Current ticket count: 99 sold 1 thread name: t2

Current ticket count: 98 sold 2 thread name: t3

Current ticket count: 97 sold 3 thread names: t4

Current ticket count: 96 sold 4 thread names: t1

Current ticket count: 95 sold 5 thread names: t2

Current ticket count: 94 sold 6 thread names: t3

Current ticket count: 93 sold 7 thread names: t4

Current ticket count: 92 sold 8 thread names: t1

Current ticket count: 91 sold 9 thread name: t2

Current ticket count: 90 sold 10 thread names: t3

Current ticket count: 89 sold 11 thread name: t4

Current number of votes: 88 sold 12 thread name: t1

Current ticket count: 87 sold 13 thread names: t2

Current ticket count: 86 sold 14 thread name: t3

Current number of votes: 85 sold 15 thread names: t4

Current ticket count: 84 sold 16 thread names: t1

Current ticket count: 83 sold 17 thread name: t2

Current ticket count: 82 Sold 18 thread names: t3

Current ticket count: 81 sold 19 thread names: t4

Current ticket count: 80 sold 20 thread names: t1

Current ticket count: 79 sold 21 thread name: t2

Current ticket count: 78 sold 22 thread name: t3

Current ticket count: 77 sold 23 thread name: t4

Current number of votes: 76 sold 24 thread name: t1

Current ticket count: 75 sold 25 thread name: t2

Current ticket count: 74 sold 26 thread names: t3

Current ticket count: 73 sold 27 thread names: t4

......

 

The same implementation can be performed on the iphone. The Iphone's Frameworks/Foundation. framework supports multi-threaded programming. The interface is defined in:

/Xcode3.1.4/Platforms/iPhoneSimulator. platform/Developer/SDKs/iPhoneSimulator3.1.sdk/System/Library

/Frameworks/Foundation. framework/Headers/NSThread. h

Open this file in the corresponding directory, and you can see that the vast majority of java interfaces can also find the corresponding implementation here, as shown below:

 

/* NSThread. h

Copyright (c) 1994-2007, Apple Inc. All rights reserved.

*/

 

# Import <Foundation/NSObject. h>

# Import <Foundation/NSDate. h>

 

@ Class NSArray, NSMutableDictionary, NSDate;

 

@ Interface NSThread: NSObject {

@ Private

Id_private;

Uint8_t _ bytes [44];

}

 

+ (NSThread *) currentThread;

 

+ (Void) detachNewThreadSelector :( SEL) selector toTarget :( id) target withObject :( id) argument;

 

+ (BOOL) isMultiThreaded;

 

-(NSMutableDictionary *) threadDictionary;

 

+ (Void) sleepUntilDate :( NSDate *) date;

+ (Void) sleepForTimeInterval :( NSTimeInterval) ti;

 

+ (Void) exit;

 

# If MAC_ OS _X_VERSION_10_2 <= MAC_ OS _X_VERSION_MAX_ALLOWED

+ (Double) threadPriority;

+ (BOOL) setThreadPriority :( double) p;

# Endif

 

# If MAC_ OS _X_VERSION_10_5 <= MAC_ OS _X_VERSION_MAX_ALLOWED

 

+ (NSArray *) callStackReturnAddresses;

 

-(Void) setName :( NSString *) n;

-(NSString *) name;

 

-(NSUInteger) stackSize;

-(Void) setStackSize :( NSUInteger) s;

 

-(BOOL) isMainThread;

+ (BOOL) isMainThread; // reports whether current thread is main

+ (NSThread *) mainThread;

 

-(Id) init; // designated initializer

-(Id) initWithTarget :( id) target selector :( SEL) selector object :( id) argument;

 

-(BOOL) isExecuting;

-(BOOL) isFinished;

 

-(BOOL) isCancelled;

-(Void) cancel;

 

-(Void) start;

 

-(Void) main; // thread body method

 

# Endif

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.