简单票务系统

简单票务系统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 
@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 

@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;
        }
    }
});

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

你可能感兴趣的:(简单票务系统)