JAVA实验 多线程的使用

实验内容:

1.哈尔滨火车站下面有三个火车票代售点,假如哈尔滨到北京的火车票总共是200张,如何用程序来实现三个售票点同时卖票的功能。

2.设计5个人排队买票,并规定买票规则和排队顺序。


代码实现:

package cn.edu.nefu.zhihu.xiancheng;

public class ChuangKou implements Runnable{
	int m=200;
	public void run() {
		while(m>0){
			synchronized(this){
				if(m>0)
				System.out.println(Thread.currentThread().getName()+"窗口卖出一张后,"+"总票数还剩票数: "+(--m));
				try{
				Thread.sleep(10);
				}
				catch(InterruptedException e){}
			}
		}
	}
}


package cn.edu.nefu.zhihu.xiancheng;

public class Test {

	public static void main(String[] args) {
		ChuangKou ck=new ChuangKou();
		Thread t1=new Thread(ck,"ck1");
		Thread t2=new Thread(ck,"ck2");
		Thread t3=new Thread(ck,"ck3");
		t1.start();
		t2.start();
		t3.start();
		while(true){
			if(ck.m==0){
				System.out.println("票已卖完!");
				break;
			}
		}
	}
}


package cn.edu.nefu.pack1;

public class A{
    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,wang,li;  
        zhang=new Thread(canema);
        sun=new Thread(canema);
        zhao=new Thread(canema);
        wang=new Thread(canema);
        li=new Thread(canema);
        zhang.setName(s1);
        sun.setName(s2);
        zhao.setName(s3);
        wang.setName(s4);
        li.setName(s5);
        zhang.start();
        sun.start();
        zhao.start();
        wang.start();
        li.start();
    }
}
class Cinema implements Runnable{    //实现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(10);
        }
    }
}
class TicketSeller{         //负责卖票的类
    int fiveNumber=3,tenNumber=1,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();  //如果线程占有CPU期间执行了wait(),就进入中断状态
                    System.out.println(s+"ppppp结束等待,继续买票");
               }
               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();  //如果线程占有CPU期间执行了wait(),就进入中断状态
                    System.out.println(s+"ooooo结束等待,继续买票");
               }
               catch(InterruptedException e){}
           }
           fiveNumber=fiveNumber-1;
           tenNumber=tenNumber-1;
           twentyNumber=twentyNumber+1;
           System.out.println(s+"给售票员20元钱,售票员卖给"+s+"一张票,找赎15元");
        }
        notifyAll();
    }
}






你可能感兴趣的:(java,多线程)