java线程

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class TicketSeller {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("请输入电影票价格:");
        int ticketPrice = scanner.nextInt();
        
        System.out.print("请输入售票员持有金额:");
        int cash = scanner.nextInt();
        
        System.out.print("请输入买票人数:");
        int numCustomers = scanner.nextInt();
        
        Customer[] customers = new Customer[numCustomers];
        
        // 依次输入买票人名和对应支付金额
        for (int i = 0; i < numCustomers; i++) {
            System.out.print("请输入第" + (i+1) + "个买票人名字:");
            String name = scanner.next();
            
            System.out.print("请输入第" + (i+1) + "个买票人支付金额:");
            int payment = scanner.nextInt();
            
            customers[i] = new Customer(name, payment);
        }
        
        scanner.close();
        
        Seller seller = new Seller(ticketPrice, cash, customers);
        seller.startSelling();
        
        // 输出买票人名和对应找零金额
        for (Customer customer : customers) {
            System.out.println(customer.getName() + " 找零金额:" + customer.getChange());
        }
    }
}

// 买票人类
class Customer {
    private String name;
    private int payment;
    private int change;

    public Customer(String name, int payment) {
        this.name = name;
        this.payment = payment;
    }
    
    public String getName() {
        return name;
    }
    
    public int getPayment() {
        return payment;
    }
    
    public void setChange(int change) {
        this.change = change;
    }
    
    public int getChange() {
        return change;
    }
}

// 售票员类
class Seller extends Thread {
    private int ticketPrice;
    private int cash;
    private Queue<Customer> queue;
    
    public Seller(int ticketPrice, int cash, Customer[] customers) {
        this.ticketPrice = ticketPrice;
        this.cash = cash;
        this.queue = new LinkedList<>();
        
        // 将买票人按照排队顺序加入队列
        for (Customer customer : customers) {
            queue.offer(customer);
        }
    }
    
    public void startSelling() {
        start();
    }

    @Override
    public void run() {
        // 执行卖票过程
        while (!queue.isEmpty()) {
            Customer customer = queue.peek();
            int payment = customer.getPayment();
            
            // 如果售票员持有金额不足以找零,则继续下一个买票人
            if (payment - ticketPrice > cash) {
                queue.poll();
                continue;
            }
            
            // 计算找零金额
            int change = payment - ticketPrice;
            
            synchronized (this) {
                // 如果售票员持有金额不足以找零,则等待
                while (change > cash) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                
                // 售票员找零
                cash -= change;
                customer.setChange(change);
                
                System.out.println(customer.getName() + " 找零金额:" + change);
                
                // 唤醒等待的线程
                notifyAll();
            }
            
            // 出队买票人
            queue.poll();
        }
    }
}

完成版

package work;

public class TicketSelling {
	public static void main(String args[]) {
		String s1 = "张三", s2 = "孙大名", s3 = "赵中堂", s4 = "李四", s5 = "王五";
		Cinema canema = new Cinema(s1, s2, s3, s4, s5);
		Thread zhang, sun, zhao, si, wu;
		zhang = new Thread(canema);
		sun = new Thread(canema);
		zhao = new Thread(canema);
		si = new Thread(canema);
		wu = new Thread(canema);
		zhang.setName(s1);
		sun.setName(s2);
		zhao.setName(s3);
		si.setName(s4);
		wu.setName(s5);
		zhang.start();
		sun.start();
		zhao.start();
		si.start();
		wu.start();
	}
}

class Cinema implements Runnable {
	TicketSeller seller;
	String name1, name2, name3, name4, name5;

	Cinema(String s1, String s2, String s3, String s4, String s5) {
		seller = new TicketSeller();
		name1 = s1;
		name2 = s2;
		name3 = s3;
		name4 = s4;
		name5 = s5;
	}

	public void run() {
		if (Thread.currentThread().getName().equals(name1)) {
			seller.sellTicket(20);
		} 
		else if (Thread.currentThread().getName().equals(name2)) {
			seller.sellTicket(10);
		} 
		else if (Thread.currentThread().getName().equals(name3)) {
			seller.sellTicket(5);
		}
		else if (Thread.currentThread().getName().equals(name4)) {
			seller.sellTicket(10);
		}
		else if (Thread.currentThread().getName().equals(name5)) {
			seller.sellTicket(5);
		}
	}
}

class TicketSeller {
	int fiveNumber = 1, tenNumber = 0, twentyNumber = 0;

	public synchronized void sellTicket(int receiveMoney) {
		String s = Thread.currentThread().getName();
		if (receiveMoney == 5) {
			fiveNumber = fiveNumber + 1;
			System.out.println(s + "for seller 5 yuan, seller sell to" + s + "one ticket, no change");
		} else if (receiveMoney == 10) {
			while (fiveNumber < 1) {
				try {
					System.out.println(s + "give 10 yuan to seller");
					System.out.println("seller please" + s + "wait a monment");
					wait();
					System.out.println(s + "on his turn, can buy ticket");
				} catch (InterruptedException e) {
				}
			}
			fiveNumber = fiveNumber - 1;
			tenNumber = tenNumber + 1;
			System.out.println(s + "give 10 yuan to seller, seller sell " + s + "one ticket, change 5 yuan");
		} else if (receiveMoney == 20) {
			while (fiveNumber < 1 || tenNumber < 1) {
				try {
					System.out.println(s + "give 20 yuan to seller");
					System.out.println("seller please" + s + "wait a monment");
					wait();
					System.out.println(s + "on his turn, can buy ticket");
				} catch (InterruptedException e) {
				}
			}
			fiveNumber = fiveNumber - 1;
			tenNumber = tenNumber - 1;
			twentyNumber = twentyNumber + 1;
			System.out.println(s + "give 20 yuan to seller, seller sell " + s + "one ticket, change 15 yuan");
		}
		notifyAll();
	}
}

中文

package work;

public class TicketSelling {
	public static void main(String args[]) {
		String s1 = "张三", s2 = "孙大名", s3 = "赵中堂", s4 = "李四", s5 = "王五";
		Cinema canema = new Cinema(s1, s2, s3, s4, s5);
		Thread zhang, sun, zhao, si, wu;
		zhang = new Thread(canema);
		sun = new Thread(canema);
		zhao = new Thread(canema);
		si = new Thread(canema);
		wu = new Thread(canema);
		zhang.setName(s1);
		sun.setName(s2);
		zhao.setName(s3);
		si.setName(s4);
		wu.setName(s5);
		zhang.start();
		sun.start();
		zhao.start();
		si.start();
		wu.start();
	}
}

class Cinema implements Runnable {
	TicketSeller seller;
	String name1, name2, name3, name4, name5;

	Cinema(String s1, String s2, String s3, String s4, String s5) {
		seller = new TicketSeller();
		name1 = s1;
		name2 = s2;
		name3 = s3;
		name4 = s4;
		name5 = s5;
	}

	public void run() {
		if (Thread.currentThread().getName().equals(name1)) {
			seller.sellTicket(20);
		} 
		else if (Thread.currentThread().getName().equals(name2)) {
			seller.sellTicket(10);
		} 
		else if (Thread.currentThread().getName().equals(name3)) {
			seller.sellTicket(5);
		}
		else if (Thread.currentThread().getName().equals(name4)) {
			seller.sellTicket(10);
		}
		else if (Thread.currentThread().getName().equals(name5)) {
			seller.sellTicket(5);
		}
	}
}

class TicketSeller {
	int fiveNumber = 1, tenNumber = 0, twentyNumber = 0;

	public synchronized void sellTicket(int receiveMoney) {
		String s = Thread.currentThread().getName();
		if (receiveMoney == 5) {
			fiveNumber = fiveNumber + 1;
			System.out.println(s + " 给售票员5元, 售票员卖给 " + s + " 一张票,没有找零");
		} else if (receiveMoney == 10) {
			while (fiveNumber < 1) {
				try {
					System.out.println(s + " 给售票员10元");
					System.out.println("售票员请  " + s + " 等待一会儿");
					wait();
					System.out.println(s + " 轮到了,可以买票");
				} catch (InterruptedException e) {
				}
			}
			fiveNumber = fiveNumber - 1;
			tenNumber = tenNumber + 1;
			System.out.println(s + " 给售票员10元,售票员卖给 " + s + " 一张票,找零5元");
		} else if (receiveMoney == 20) {
			while (fiveNumber < 1 || tenNumber < 1) {
				try {
					System.out.println(s + " 给售票员20元");
					System.out.println("售票员请  " + s + " 等待一会儿");
					wait();
					System.out.println(s + " 轮到了,可以买票");
				} catch (InterruptedException e) {
				}
			}
			fiveNumber = fiveNumber - 1;
			tenNumber = tenNumber - 1;
			twentyNumber = twentyNumber + 1;
			System.out.println(s + " 给售票员20元,售票员卖给 " + s + " 一张票,找零15元");
		}
		notifyAll();
	}
}