简单票务系统

简单票务系统JAVA&OC对比实现

最近在学习JAVA,其中有一个票务系统,和OC中实现的原理有些不同,趁兴也用OC写一个,对比一下,也帮助学习同异步使用。

下面是JAVA实现:
其中需要两个类Reservoir(票) Booth(售票厅)

//票
public class Reservoir {
    private int total;

public Reservoir(int t) {
    this.total = t;
}
public synchronized boolean sellTicket() {
    if(this.total > 0) {
        this.total --;
        return true;//成功售出一
    }else {
        return false;//没有票了
    }
  }
}

//售票厅
public class Booth extends Thread {
private static int threadID = 0;//属于类
private Reservoir release;//售票
private int count = 0;//属于线程

public Booth(Reservoir re) {
    super("ID:"+ (++threadID));
    this.release = re;//所有售票厅共用一个票
    this.start();
}
public String toString() {
    return super.getName();
}
public void run() {
    while(true) {
        if(this.release.sellTicket()) {
            this.count = this.count + 1;
            System.out.println(this.getName() + ": sell 1");
            try {
                sleep((int)Math.random() * 100);
            }
            catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }else {
            break;
        }
    }
    System.out.println(this.getName() + "I sold:" + count);
}
}

使用:
   Reservoir re = new Reservoir(100);
    Booth b1 = new Booth(re);
    Booth b2 = new Booth(re);
    Booth b3 = new Booth(re);
    
    输出:
    ID:2: sell 1
    ID:3: sell 1
    ID:1: sell 1
    ID:3: sell 1
    ID:2: sell 1
    ....
    ID:1: sell 1
    ID:2I sold:37
    ID:1I sold:31
    ID:3I sold:32

OC对比

ticket类  Booth类
#import <Foundation/Foundation.h>
@interface Ticket : NSObject
- (BOOL)sellTicket;
- (BOOL)ticketCount;
@end
    
#import "Ticket.h"

@interface Ticket ()
    
@end

@implementation Ticket

static NSInteger count = 100;

- (BOOL)sellTicket {

@synchronized (self) {
     if (count == 0) {
         return NO;
        }else {
         count --;
         return YES;
        }
    }
}

- (BOOL)ticketCount {

    if (count == 0) {
        return NO;
    }else {
        return YES;
    }
}

@end

#import <Foundation/Foundation.h>

@interface Booth : NSObject

- (instancetype)initWithticket:(id)ticket;


- (BOOL)sellTicket;

@end

#import "Booth.h"
#import "Ticket.h"

@interface Booth ()
@property(nonatomic,strong)Ticket *ticket;
@property(nonatomic,assign)NSInteger count;
@property(nonatomic,assign)NSInteger boothID;
@end

@implementation Booth

static NSInteger boothID = 0;

- (instancetype)initWithticket:(Ticket *)ticket {

    self = [super init];
    if (self) {
        boothID = boothID + 1;
        _boothID = boothID;
        self.ticket = ticket;
        _count = 0;
   }
   return self;
}

- (BOOL)sellTicket {

while (YES) {
    if ([self.ticket sellTicket]) {
        NSLog(@"ID:%ld 售出一张",_boothID);
        _count ++;
        return YES;
    }else {
        NSLog(@"ID:%ld 共售出%ld",_boothID,_count);
        return NO;
        break;
    }
}
}
@end


使用:controller中
    Ticket *ticket = [[Ticket alloc] init];
Booth *booth1 = [[Booth alloc] initWithticket:ticket];
Booth *booth2 = [[Booth alloc] initWithticket:ticket];
Booth *booth3 = [[Booth alloc] initWithticket:ticket];

dispatch_queue_t queue = dispatch_queue_create("com.sellticket.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{

    while (YES) {
        if ([booth1 sellTicket]) {
        }else {
            break;
        }
    }
});
dispatch_async(queue, ^{
    
    while (YES) {
        if ([booth2 sellTicket]) {
        }else {
            break;
        }
    }
});
dispatch_async(queue, ^{
    
    while (YES) {
        if ([booth3 sellTicket]) {
        }else {
            break;
        }
    }
});

感觉差了一些 具体哪里差了又说不上来 啧啧啧

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文写于2017年7月,参考了很多网络资料,记得的都已在文中标明出处。 【内容提要】 行业现状:市场巨大,经济体量...
    花哥钱串串阅读 7,425评论 1 18
  • 有大海就有沙滩,美女,比基尼。在炎热的夏天穿上心爱的泳装,沙滩裙面对着大海心也会平静下来,想要在这种美女盛行...
    张惠惠betty阅读 347评论 18 0
  • 我们的爱情已经死了 在你最后吻我的那一刻 只不过是回光返照 而我却以为是永恒
    小紫馨阅读 153评论 0 0
  • 转发一篇文章,对沙盘治疗的分析研究很有借鉴意义。但是正下作者说的那样他更多的是分析的游戏治疗.沙盘游戏治疗而不是沙...
    东风001001阅读 1,053评论 0 0
  • 我在那暖暖的阳光中等你 在那寒冷的雨夜等你 在那灰色的天空下等你 终于,我等来了一个人 渐渐地 他近了,近了 可我...
    南苑i阅读 247评论 4 1