侵权投诉
订阅
纠错
加入自媒体

利用iphone的多线程实现“售票系统”

2011-01-14 16:06
kumsing
关注

  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

  @end

  FOUNDATION_EXPORT NSString * const NSWillBecomeMultiThreadedNotification;

  FOUNDATION_EXPORT NSString * const NSDidBecomeSingleThreadedNotification;

  FOUNDATION_EXPORT NSString * const NSThreadWillExitNotification;

  @interface NSObject (NSThreadPerformAdditions)

  #if MAC_OS_X_VERSION_10_2 《= MAC_OS_X_VERSION_MAX_ALLOWED

  - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array;

  - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;

  // equivalent to the first method with kCFRunLoopCommonModes

  #endif

  #if MAC_OS_X_VERSION_10_5 《= MAC_OS_X_VERSION_MAX_ALLOWED

  - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array;

  - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;

  // equivalent to the first method with kCFRunLoopCommonModes

  - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg;

  #endif

  @end

1  2  3  4  下一页>  
声明: 本文由入驻维科号的作者撰写,观点仅代表作者本人,不代表OFweek立场。如有侵权或其他问题,请联系举报。

发表评论

0条评论,0人参与

请输入评论内容...

请输入评论/评论长度6~500个字

您提交的评论过于频繁,请输入验证码继续

暂无评论

暂无评论

文章纠错
x
*文字标题:
*纠错内容:
联系邮箱:
*验 证 码:

粤公网安备 44030502002758号