Multi-threaded mock ticketing

Source: Internet
Author: User

Demand: A movie theater for sale of certain films (3, Red sorghum ...), three windows at the same time to make tickets (100 tickets), please design a program, analog cinema ticketing
Two different ways:
Inherited
Interface
(1) Synchronized implementation
public class Sellticketdemo {

public static void main(String[] args) {//创建资源类对象(共享资源类/目标对象)    SellTicket st = new SellTicket() ;    //创建线程类对象    Thread t1 = new Thread(st, "窗口1") ;    Thread t2 = new Thread(st ,"窗口2") ;    Thread t3 = new Thread(st, "窗口3") ;    //启动线程    st1.start();    st2.start();    st3.start();}

}
public class Sellticket implements Runnable {

//定义100张票private int tickets = 100 ;private Object obj = new Object() ;@Overridepublic void run() {    while(true) {        //new Object():锁对象 (门和关)        //t1,t2,t3        synchronized(obj) {//t1进来,门一关,t2,t3进不来了            if(tickets>0) {                try {                    //睡眠:延迟                    Thread.sleep(100);                 } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(Thread.currentThread().getName()                        +"正在出售第"+(tickets--)+"张票");//0,-1            }        }    }}

}
(2) Lock implementation
Test class
public class Sellticketdemo {

public static void main(String[] args) {    SellTicket st = new SellTicket() ;    Thread t1 = new Thread(st,"窗口1") ;    Thread t2 = new Thread(st,"窗口2") ;    Thread t3 = new Thread(st,"窗口3") ;    //启动线程    t1.start();     t2.start();     t3.start();}

}
Import Java.util.concurrent.locks.Lock;
Import Java.util.concurrent.locks.ReentrantLock;

public class Sellticket implements Runnable {

// 定义票private int tickets = 100;// Object obj = new Object();// Jdk5.0以后,java提供了一个具体的锁: 接口:Lockprivate Lock lock= new ReentrantLock(); //显示获取锁的前提,一定要创建Lock接口对象@Overridepublic void run() {    while (true) {        try { //try...finally            lock.lock(); // 获取锁    syncrhonized(obj)            if (tickets > 0) {                try {                    Thread.sleep(100);                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(Thread.currentThread().getName() + "正在出售第" + (tickets--) + "张票");            }        } finally {//释放锁            if(lock!=null) {                lock.unlock();            }        }    }}

}

Multi-threaded mock ticketing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.