当年第一次学习java多线程写的例子,从此多线程不再陌生

public class Yi_Hong_Courtyard {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Yi_Hong_Courtyard yunxing = new Yi_Hong_Courtyard();
 
    }
 
 
    Yi_Hong_Courtyard(){
         
        Procuress procuress = new Procuress();
         
        Miss miss = new Miss(procuress);
        Piao_Guest guest = new Piao_Guest(procuress);
         
        Thread t_miss = new Thread(miss);
        Thread t_guest = new Thread(guest);
         
        Produce_Business();//内容介绍{函数在29--31行}
         
        t_miss.start();
        t_guest.start();
         
    }
     
    public void Produce_Business(){
        System.out.println("欢迎光临大日本怡红院-----祝您玩的愉快(xi wang nin zai e gao zhong xue hui duo xian cheng)");
    }
}
 
//小姐...给钱就干
class Miss implements Runnable{
 
    Procuress procuress;
     
    public Miss(Procuress procuress){
        this.procuress = procuress;
    }
     
    @Override
    public void run() {
        // TODO Auto-generated method stub
        int i = 1;
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if(i == 1){
                try {
                    procuress.fenpen_miss("苍井空","500日元");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else {
                try {
                    procuress.fenpen_miss("甜心花子", "800日元");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            i = (i+1) % 2;
        }
    }
     
}
 
//嫖客...花钱就嫖
class Piao_Guest implements Runnable{
 
    Procuress procuress;
     
    public Piao_Guest(Procuress procuress){
        this.procuress = procuress;
    }
     
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                procuress.make_love();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
     
}
 
//老鸨...统一管理小姐内部工作
class Procuress{
     
    private String miss = null;
    private String price = null;
    private boolean jie_Guest = true;
     
    public synchronized void fenpen_miss(String miss,String price) throws InterruptedException{
        if(!jie_Guest)
            wait();//等待
        this.miss = miss;
        this.price = price;
        jie_Guest = false;
        notify();//叫醒
    }
     
    public synchronized void make_love() throws InterruptedException{
        if(jie_Guest)
            wait();//等待
        System.out.println("接客小姐:"+miss);
        System.out.println("接客价格:"+price);
        System.out.println("  "+"  "+"  "+"  "+"  "+"  "+"  "+"  "+"  "+"  "+miss+"工作完事"+"准备接客... ...");
        System.out.println("****************************************");
        jie_Guest = true;
        notify();//叫醒
    }
}

 

小傅哥 CSDN认证博客专家 ASM 设计模式 面经手册
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付 39.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值