[ 登录注册 ]

ios

利用iphone的多线程实现“售票系统”(手把手教你iphone开发 - 基

2016/08/11 11:19 [db:作者] 返回上一页

作者:孙东风 2009-11-10(请尊重作者劳动成果,转载务必注明出处)

 

Java因为其本身支持多线程而给程序员带来很多方便,其实在iphone的开发中也支持多线程编程,并且一点也不比java麻烦。

在这篇文章中,笔者就拿大多数Java教程中经典的“售票系统多线程”作为实际例子,在iphone中进行同样的实现。

 

下面是java版本的“售票系统多线程”代码:

package demo;  

 

public class SellTickets implements Runnable{  

      

    private int tickets=100;  

      

    public void run() {  

          

        int count=0;  

          

        while (true)  

        {  

            //上锁  

            synchronized(this){  

            if (tickets>0){  

                try {  

                    Thread.sleep(500);  

                } catch (InterruptedException e) {  

                    e.printStackTrace();  

                }  

                count=100-tickets;  

                System.out.println("当前票数是:"+tickets+"售出"+count  

                        +"线程名:"+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();       

    }  

 

以上java版本的代码执行后控制台输出如下:

当前票数是:100售出0线程名:t1

当前票数是:99售出1线程名:t2

当前票数是:98售出2线程名:t3

当前票数是:97售出3线程名:t4

当前票数是:96售出4线程名:t1

当前票数是:95售出5线程名:t2

当前票数是:94售出6线程名:t3

当前票数是:93售出7线程名:t4

当前票数是:92售出8线程名:t1

当前票数是:91售出9线程名:t2

当前票数是:90售出10线程名:t3

当前票数是:89售出11线程名:t4

当前票数是:88售出12线程名:t1

当前票数是:87售出13线程名:t2

当前票数是:86售出14线程名:t3

当前票数是:85售出15线程名:t4

当前票数是:84售出16线程名:t1

当前票数是:83售出17线程名:t2

当前票数是:82售出18线程名:t3

当前票数是:81售出19线程名:t4

当前票数是:80售出20线程名:t1

当前票数是:79售出21线程名:t2

当前票数是:78售出22线程名:t3

当前票数是:77售出23线程名:t4

当前票数是:76售出24线程名:t1

当前票数是:75售出25线程名:t2

当前票数是:74售出26线程名:t3

当前票数是:73售出27线程名:t4

……

 

可以在iphone中进行同样的实现,Iphone的Frameworks/Foundation.framework框架支持多线程编程,接口定义在:

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

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

到相应的目录下打开此文件,可以看到绝大多数java中的接口这里也都能找到相应的实现,如下:

 

/*  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

点击复制链接 与好友分享!回本站首页

文章来源:http://www.bozhiyue.com/ios/2016/0811/364107.html
评论
发表评论