多线程的应用

实验内容
1.设计5个人排队买票,并规定买票规则和排队顺序。

public 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();  //如果线程占有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();
    }
}
public 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(10);
        }
    }
}
public class Main {
	public static void main(String[] args) {
		String s1="张三",s2="孙大名",s3="赵中堂",s4="张睿B",s5="张R斌";
        Cinema canema=new Cinema(s1,s2,s3,s4,s5);
        Thread zhang,sun,zhao,RRR,BBB;  
        zhang=new Thread(canema);
        sun=new Thread(canema);
        zhao=new Thread(canema);
        RRR=new Thread(canema);
        BBB=new Thread(canema);
        zhang.setName(s1);
        sun.setName(s2);
        zhao.setName(s3);
        RRR.setName(s4);
        BBB.setName(s5);
        zhang.start();
        sun.start();
        zhao.start();
        BBB.start();
        RRR.start();
	}
}


2. 编写一个应用程序,在主线程中创建三个线程:“运货司机”“装运工”“仓库管理员”。要求线程“运货司机”占有CPU资源后立刻联合线程“装运工”,而“装运工”占有资源后立刻联合线程“仓库管理员”,打开仓库搬运货物,然后装成,运走。

public class company implements Runnable {
	Thread 运货司机, 装运工, 仓库管理员;
	company() {
		运货司机 = new Thread(this);
		装运工 = new Thread(this);
		仓库管理员 = new Thread(this);
	}
	public void run() {
		if (Thread.currentThread() == 运货司机) {
			System.out.println("运货司机要提货,联系装运工。。。。。。。。。。。。");
			装运工.start();
			while (装运工.isAlive() == false) {
			}
			try {
				装运工.join();
			} catch (InterruptedException e) {
			}
			System.out.println("运货公司收到装运工的货,开始运货。。。。。。。。。");
		} else if (Thread.currentThread() == 装运工) {
			System.out.println("装运工需要打开仓库,联系仓库管理员。。。。。。。。");
			仓库管理员.start();
			while (仓库管理员.isAlive() == false) {
			}
			try {
				仓库管理员.join();
			} catch (InterruptedException e) {
			}
			System.out.println("仓库管理员打开了仓库,装运工取出了货物。。。。。。");
		} else if (Thread.currentThread() == 仓库管理员) {
			System.out.println("仓库管理员去打开仓库,需等待5秒。。。。。。。。。");
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
			}
		}
	}
}
public class Main {
	public static void main(String[] args) {
		company a=new company();
		a.运货司机.start();
	}
}



posted @ 2016-04-28 17:06  martinue  阅读(238)  评论(0编辑  收藏  举报