Java multithreaded simple project--Cinema ticketing machine

Source: Internet
Author: User
Tags thread class ticket

Look at the Internet so much information and video, probably also know how to use the thread API, then do a project to test the knowledge of it ~ ~

The following is a test class for setting parameters and starting threads, nothing to say. (Knowledge point: Create thread object, thread open)

Package com.thread.movticket; * Movie ticket system thread Test * Usage: Sleep,synchronic,join,wait,notify,start,setpriority,yield * Business functions: * VIP Purchase tickets, the civilian purchase tickets (join,wait, 
    	Notify)/public class Movietest {public static void main (string[] args) {//movie ticket number int ticketamount = 1000;
    	Create a movie ticketing system based on the number of votes moviesystem moviesystem = new Moviesystem (ticketamount);
		try {thread.sleep (1000);
		catch (Interruptedexception E1) {//TODO auto-generated catch block E1.printstacktrace (); //Open three Windows tickets, two civilians, a VIP saleticket saleticketa =new saleticket (moviesystem,0,ticketamount);//Movie System object, VIP level,
    	Number of votes Saleticket saleticketb =new saleticket (moviesystem,0,ticketamount);
    	Saleticket vipsaleticket = new Saleticket (moviesystem,1,ticketamount);
    	
    	System.out.println ("The opening of the movie theater of the" Hu ' an goose factory);
    	thread T1 = new Thread (Saleticketa, "window A");
    	Thread t2 = new Thread (Saleticketa, "Window B");

    	thread t3 = new Thread (Saleticketa, "VIP window");
    	T1.start ();
    	T2.start ();T3.start (); }
}

The following is the main class (Knowledge point: Sleep ())

Package com.thread.movticket;

public class Moviesystem {
	
	//movie tickets. Total number of movie tickets
	private int ticketamount;
	Private int[] ticket;
	
	Public Moviesystem (int ticketamount) {
		
		//Instantiate ticket number Create array
		this.ticketamount = Ticketamount;
		Ticket = new Int[ticketamount];
		
		The ticket is divided by seat number () for
		(int i=0;i<this.ticketamount;i++) {
			ticket[i] = i+1;
		}
	}
	*
	 * * Selling Method
	 * Ticket number is more than 0 to sell tickets, TicketAmount-1 is because the number of votes than the array of 1, otherwise told to return-1 ———— "No Ticket ~ ~"
	 * Each sold a ticket, the number of votes minus one, The current thread hibernation 400ms represents the system processing information (in fact, to allow us to better observe the output)
	 * Results return the number of tickets (also seat number, forgive me lazy)/public
	int Saleticket () {
		
		if (ticketamount>0) {
			if (Ticket[ticketamount-1] > 0) {
			ticketamount--;
			try {
				thread.sleep;
			} catch (Interruptedexception e) {
				e.printstacktrace ();
			}
			Return ticketamount+1
			}
			}
			return-1;//no Ticket
	}
}
Then there is the thread class knowledge point: ①keeprunning stop Thread ②setpriority () Set Thread priority ③synchronize (object o) thread's sync lock, personal feel object o useless, write on it.

Package com.thread.movticket;	
	public class Saleticket extends thread{//shared movie system, create lock object, create dominant object private Moviesystem Moviesystem;
	Object = new Object ();
Boolean keeprunning = true;
private int ticketamount;
	
	Int[] seat; Share cinema system and ticket number, set VIP level public Saleticket (Moviesystem moviesystem,int viplever,int ticketamount) {this.moviesystem = m
		
		Oviesystem;
		According to the VIP level set speed, currently only normal and VIP1 level if (Viplever = = 0) {thread.currentthread (). setpriority (min_priority);
		}else if (viplever = = 1) {Thread.CurrentThread (). setpriority (max_priority);
			} public void Run () {while (keeprunning) {//store the number of movie tickets returned by the movie system (seat number) and use synchronize to prevent multiple threads from reading the same seat number int num;
			Synchronized (object) {num = Moviesystem.saleticket ();
				if (num > 0) {System.out.println (Thread.CurrentThread (). GetName () + ": Sell" +num+ "seat");
			System.out.println ("Left" + (num-1) + "Ticket"); }else if (num = = 1) {System.out.println (Thread.CurrentThread (). GetName () + "."
				+ "No Ticket ~ ~"); Keeprunning =False }
			}
		}
	}
}
Basically these are the knowledge involved is too shallow, shame.

Related Article

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.