多线程的使用实列-卖票系统

#import "ViewController.h"

@interface ViewController ()

{

UILabel *showLable;

int curTicketNum;

int saleTicketNum;

NSString *saleWindowName;

NSCondition *conditionClock;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//卖票系统
分3个窗口同时销售

//默认100张票

curTicketNum = 100;

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(100, 20, 150, 50);

[self.view addSubview:button];

[button setTitle:@"卖票了" forState:UIControlStateNormal];

button.backgroundColor = [UIColor redColor];

[button addTarget:self action:@selector(startSale) forControlEvents:UIControlEventTouchUpInside];

showLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, 375, 200)];

[self.view addSubview:showLable];

showLable.numberOfLines =3;

showLable.textAlignment = NSTextAlignmentCenter;

showLable.text = @"剩余票数:100张";

showLable.font = [UIFont boldSystemFontOfSize:20];

showLable.backgroundColor = [UIColor orangeColor];

}

//三个窗口同时卖票

- (void)startSale

{

//初始化3个线程
每一个线程都是一个窗口

NSThread *firstWindow = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];

firstWindow.name = @"售票<窗口1>";

[firstWindow start];

NSThread *secondtWindow = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];

secondtWindow.name = @"售票<窗口2>";

[secondtWindow start];

NSThread *thirdWindow = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];

thirdWindow.name = @"售票<窗口3>";

[thirdWindow start];

//    NSCondition 是一个线程锁(条件锁)

conditionClock = [[NSCondition alloc]init];

}

- (void)saleTicket

{

//没有使用线程锁 3个线程(窗口)会同时访问卖票的方法
当剩余的票数是0 的时候其他线程还会继续访问,不知道剩余票数为0
,继续访问剩余票数就有负数的情况

//解决这个问题可以使用线程锁;只允许一个线程访问完毕之后另外一个线程再去访问

while (curTicketNum > 0) {

//使用线程锁只允许一个线程取访问

[conditionClock lock];

saleWindowName = [NSThread currentThread].name;

[NSThread sleepForTimeInterval:0.1];

//当前票数(剩余)

curTicketNum -= 1;

//卖的票数

saleTicketNum = 100-curTicketNum;

[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];

if (curTicketNum > 0) {

[conditionClock unlock];

}

}

}

//更新界面

- (void)updateUI

{

NSLog(@"已经销售%d
张票\n剩余 %d
张票\n当前销售窗口是:%@",saleTicketNum,curTicketNum,saleWindowName);

showLable.text = [NSString stringWithFormat:@"已经销售%d
张票\n剩余 %d
张票\n当前销售窗口是:%@",saleTicketNum,curTicketNum,saleWindowName];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 11:37:48

多线程的使用实列-卖票系统的相关文章

多线程的两种方法(卖票系统展示)

public class MyThread1 implements Runnable{ int i=20; String name; public MyThread1() { // TODO 自动生成的构造函数存根 this.name=name; } public void run(){ for(int x=0;x<20;x++){ if(i>0){ System.out.print(Thread.currentThread().getName()+" "); System

java多线程实现卖票小程序

1 package shb.java.demo; 2 /** 3 * 多线程测试卖票小程序. 4 * @Package:shb.java.demo 5 * @Description: 6 * @author shaobn 7 * @Date 2015-9-2下午7:49:53 8 */ 9 public class TestSyn { 10 public static void main(String[] args) { 11 //此注释为实现方式一 12 /*TicketDemo td = n

多线程之多窗口卖票&amp;线程之间的通信

案例一:使用多线程完成三个窗口卖票(不能出现重复卖票以及负数票) 卖票程序SellTicket 这里使用Lock类中的方法实现加锁和释放锁! package cn.itcast.thread2; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class SellTicket implements Runnable { private int ticke

多线程抢票系统浅析

笔者打算写个轻量版的秒杀系统,那么需要多线程模拟客户去抢购某个商品.故有想先写一个简单的多线程抢票系统加深一下对线程池,同步的理解. 1. 新建Java project,命名为ClientApp1, src文件夹里面新建demo文件夹. 项目结构如下, 2. 程序模拟的场景用例如下, 多个线程模拟多个客户去购买春运车票 每个客户购买车票[0,9],最少买0张,最多能买九张. 每个客户同步的买票,当某个线程在买票时,其他线程处于等待状态 所有客户线程买票完毕,主线程最后统计一共卖出多少张车票,切忌

java多线程实现卖票程序

本文采用java多线程实现了模拟车站多个车票卖票的功能. 关键词:java多线程 共享变量 实现runnable接口 volatile  线程同步. 代码如下 Ticket类 package ex7_TicketSaler; /*同一对象的多个线程thread0/1/2,对共享变量count的操作,需要将count的值声明为volatile * 并且因为多个线程操作的是同一个对象ticket,因此count是资源共享的 * */ public class Ticket implements Ru

多线程(多窗口卖票例子)

实现多线程的方式: 实现多线程的方式有多种,这里只列举两种常用的,而第一种继承Thread的方式无法实现多窗口卖票. 一,继承Thread方式: 特点:多线程多实例,无法实现资源的共享. 例子: 1 package com.demo.study.multithreading; 2 3 public class MyThread extends Thread{ 4 5 private int i = 10; 6 // 可以自行定义锁,也可以使用实例的锁 7 Object mutex = new O

多线程系列1:经典卖票

1.卖票的方法 class TicketRest { int ticket = 1; int Max = 0; public TicketRest(int max) { Max = max; } /// <summary> /// 未加锁 /// </summary> /// <param name="num"></param> public void SellTicketNoLock(int num) { while (ticket &

实现多线程的两种方式,卖票场景,亲测可用

写在开始 卖票场景: 多线程共同卖票,总票数在多个卖票窗口共享 实现方式: 1继承Thread类: 2实现Runnable接口 正文开始 方式1 Thread继承 package com.example.demo; /** * @Description: * @Author:tianminghai * @Date:2:55 PM 2018/10/24 */ public class ThreadDemo extends Thread { private static int ticket = 1

多线程卖票代码

package com.loaderman.syn; public class Demo_Ticket { /** * 需求:铁路售票,一共100张,通过四个窗口卖完. */ public static void main(String[] args) { new Ticket().start(); new Ticket().start(); new Ticket().start(); new Ticket().start(); } } class Ticket extends Thread {