大数据Java基础第八天作业
2016-05-13 18:07
337 查看
第一题: class Car extends Thread{ private String name; private Locked lock; public Car(String name,Locked lock){ this.name = name; this.lock = lock; } public void run(){ synchronized(lock){ System.out.println("车号:" + name + ",开始进入隧道..."); for(int i=1;i<=10;i++){ try{ System.out.println("进入隧道第" + i + "秒"); Thread.sleep(1000*1); }catch(Exception e){}; } System.out.println("车号:"+ name + ",结束进入隧道..."); } } } class Locked{ //空锁... } class CarDemo{ public static void main(String[] args){ Locked lock = new Locked(); Car car001 = new Car("car001",lock); Car car002 = new Car("car002",lock); Car car003 = new Car("car003",lock); Car car004 = new Car("car004",lock); Car car005 = new Car("car005",lock); car001.start(); car002.start(); car003.start(); car004.start(); car005.start(); } } 第二题: import java.util.ArrayList; import java.util.List; class Bee extends Thread{ private String name; private List<Integer> list; private int i = 1; public Bee(String name,List<Integer> list){ this.name = name; this.list = list; } public void run(){ while(true){ synchronized(list){ if(list.size() >= 10){ try{ list.wait(); }catch(Exception e){}; }else{ try{ Thread.sleep(1000); }catch(Exception e){}; System.out.println("蜜蜂号:" + name + ",生产第" + i + "次蜂蜜!"); list.add(new Integer(i)); i ++; list.notify(); } } } } } class Bear extends Thread{ private String name; private List<Integer> list; private int total = 1; private Locked lock; public Bear(String name,List<Integer> list){ this.name = name; this.list = list; } public void run(){ while(true){ synchronized(list){ if(list.size() >= 10){ System.out.println("熊消费:" + total + "次!"); total ++; list.removeAll(list); try{ Thread.sleep(2000); }catch(Exception e){}; list.notify(); }else{ try{ list.wait(); }catch(Exception e){ e.printStackTrace(); } } } } } } class ProductDemo{ public static void main(String[] args){ List<Integer> list = new ArrayList<Integer>(); Bee bee001 = new Bee("001",list); Bee bee002 = new Bee("002",list); Bee bee003 = new Bee("003",list); bee001.start(); bee002.start(); bee003.start(); Bear bear = new Bear("001",list); bear.start(); } }
本文出自 “森林敏” 博客,请务必保留此出处http://senlinmin.blog.51cto.com/6400386/1773149
相关文章推荐
- poj1273 Drainage Ditches
- 浙大 PAT Advanced level 1014. Waiting in Line (30)
- 《最终幻想XV》中角色AI的意识决策系统解析
- 大凯哥说大数据(系列一):没有云计算就没有大数据
- RDD创建方式(集合,本地,HDFS)
- Could not obtain transaction-synchronized Session for current thread
- POJ 1363 Rails
- 人工智能
- myBaits 插入多条数据 foreach 查询数据
- 大数据量高并发访问的数据库优化方法
- main()函数的参数
- Async Programming - 1 async-await 糖的本质(2)
- 清除陷入CLOSE_WAIT的进程
- kairosdb中的配置说明
- 使用 xCAT 简化 AIX 集群的部署和管理
- svn提交报错:svn: Aborting commit:XXXXXremains in conflict
- hdu 1983 Kaitou Kid - The Phantom Thief (2)【Bfs+暴力枚举】
- HDU_1789_doing homework again_贪心
- 安装cassandra作为kairosdb的数据存储
- 【POJ 2010 Moo University-Financial Aid】优先级队列