模擬花錢賺錢

女人花錢,男人負責賺錢.女人每次花錢不超過10萬,如果餘額不夠但不爲0,則有多少就花多少,花光了就等男人賺錢.男人每次賺2萬的隨機整數倍,每次不超過10萬,如果餘額加上新賺的錢超過10萬,則保留並把錢交到女人手上.

代碼如下:

import java.util.Random;

public class Test {
    static int money = 0;
    public static void main(String[] args) {                
        Thread t1 = new Thread("女人"){
            public void run (){
                synchronized (Class.class) {
                    while(true){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        Random ra = new Random();
                                            
                            if(money<0){
                                try {
                                    Class.class.wait();
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }else{
                                Class.class.notifyAll();
                                int i =ra.nextInt(10)+1;                    
                                money-=i;
                                if(money<0){
                                    money+=i;
                                    try {
                                        Class.class.wait();
                                    } catch (InterruptedException e) {
                                        // TODO Auto        -generated catch block
                                        e.printStackTrace();
                                    }
                                }else{
                                    System.out.println(this.getName()+"花了"+i+"萬元還剩"+money+"萬元");
                                }
                                
                            }                                        
                    }
                }            
            }
        };
        Thread t2 = new Thread("男人"){
            public void run (){            
                synchronized (Class.class) {
                    while(true){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        Random ra = new Random();                                    
                            if(money>=10){
                                try {
                                    Class.class.wait();
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }else{
                                Class.class.notifyAll();
                                int i =(ra.nextInt(5)+1)*2;
                                money+=i;    
                                System.out.println(this.getName()+"賺了"+i+"萬元還剩"+money+"萬元");
                            }            
                    }
                }                
            }
        };
        t1.start();
        t2.start();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章