public class t2 { 
        public static void main(String[] args) { 
                /* 
                 *  
                 * 2.有一个抽奖池,该抽奖池中存放了奖励的金额,该抽奖池用一个数组int[] arr = 
                 * {10,5,20,50,100,200,500,800,2,80,300}; 
                 * 创建两个抽奖箱(线程)设置线程名称分别为“抽奖箱1”,“抽奖箱2”, 随机从arr数组中获取奖项元素并打印在控制台上,格式如下: 
                 *  
                 * 抽奖箱1 又产生了一个 10 元大奖 抽奖箱2 又产生了一个 100 元大奖 //..... 
                 */ 
                G g = new G(); 
                new Thread(g, "抽奖箱1").start(); 
                new Thread(g, "抽奖箱2").start(); 
 
        } 
} 
 
class G implements Runnable { 
        private int[] arr = { 10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300 }; 
        private int count = arr.length; 
 
        public void run() { 
                while (true) { 
                        synchronized (this) { 
 
                                String n = Thread.currentThread().getName(); 
                                Random r = new Random(); 
                                int s = r.nextInt(arr.length); 
                                if (count <= 0) { 
                                        break; 
                                } 
                                 
                                // 抽奖箱1 又产生了一个 10 元大奖 
                                count--; 
                                System.out.println(n + "又产生了一个" + s + "元大奖"); 
 
                        } 
                } 
 
        } 
 
} |   
        
 
    
    
    
     
 
 |