package com.hwq.arithmetic;
public class TicketOfficeTest
{
public static void main(String[] args)
{
TicketOffice officeTest = new TicketOffice();
Thread thread1 = new Thread(officeTest);
thread1.setName("售票点一");
thread1.start();
Thread thread2 = new Thread(officeTest);
thread2.setName("售票点二");
thread2.start();
Thread thread3 = new Thread(officeTest);
thread3.setName("售票点三");
thread3.start();
Thread thread4 = new Thread(officeTest);
thread4.setName("售票点四");
thread4.start();
Thread thread5 = new Thread(officeTest);
thread5.setName("售票点五");
thread5.start();
}
}
class TicketOffice implements Runnable
{
private int tickets = 0;
public void run()
{
boolean flag = true;
while(flag)
{
flag = sell();
}
}
public Boolean sell()
{
boolean flag = true;
synchronized (this)
{
if(tickets < 100)
{
tickets = tickets + 1;
System.out.println(Thread.currentThread().getName()+":"+"卖出第:"+tickets+"票");
}
else
{
flag = false;
}
}
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return flag;
}
}
public class TicketOfficeTest
{
public static void main(String[] args)
{
TicketOffice officeTest = new TicketOffice();
Thread thread1 = new Thread(officeTest);
thread1.setName("售票点一");
thread1.start();
Thread thread2 = new Thread(officeTest);
thread2.setName("售票点二");
thread2.start();
Thread thread3 = new Thread(officeTest);
thread3.setName("售票点三");
thread3.start();
Thread thread4 = new Thread(officeTest);
thread4.setName("售票点四");
thread4.start();
Thread thread5 = new Thread(officeTest);
thread5.setName("售票点五");
thread5.start();
}
}
class TicketOffice implements Runnable
{
private int tickets = 0;
public void run()
{
boolean flag = true;
while(flag)
{
flag = sell();
}
}
public Boolean sell()
{
boolean flag = true;
synchronized (this)
{
if(tickets < 100)
{
tickets = tickets + 1;
System.out.println(Thread.currentThread().getName()+":"+"卖出第:"+tickets+"票");
}
else
{
flag = false;
}
}
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return flag;
}
}