4月19日,TestCommunicate,每日20行。
来源:互联网 发布:知巳手机 编辑:程序博客网 时间:2024/05/30 02:24
class Account{ volatile private int value; volatile private boolean isMoney = false; synchronized void put(int i){ while(isMoney){ try{wait();} catch(Exception e){} } value = value + i; System.out.println("存入" + i + " 账上金额为:" + value); isMoney = true; notifyAll(); } synchronized int get(int i){ while(!isMoney){ try{wait();} catch(Exception e){} } if(value > i) value = value - i; else{ i = value; value = 0; } System.out.println("取走" + i + " 账上金额为:" + value); isMoney = false; notify(); return i; }}class Save implements Runnable{ private Account a1; public Save(Account a1){ this.a1 = a1; } public void run(){ while(true){ a1.put(100); } }}class Fetch implements Runnable{ private Account a1; public Fetch(Account a1){ this.a1 = a1; } public void run(){ while(true){ a1.get(100); } }}public class TestCommunicate { public static void main(String[] args) { Account a1 = new Account(); new Thread(new Save(a1)).start(); //两个线程存 new Thread(new Save(a1)).start(); new Thread(new Fetch(a1)).start(); //一个线程取 }}
0 0
- 4月19日,TestCommunicate,每日20行。
- 4月17日,TestCommunicate,每日20行。
- 4月18日,TestCommunicate,每日20行。
- 6月4日,每日20行。
- 3月19日,GridBagLayoutDemo,每日20行。
- 4月20日,TestVector,每日20行。
- 4月4日,Decode,每日20行。
- 3月4日,simple_window,每日20行。
- 4月1日,StringEqualTest,每日20行。
- 4月2日,StringEqualTest1,每日20行。
- 4月3日,CharCode,每日20行。
- 4月5日,StringBufferPass,每日20行。
- 4月6日,StringBufferModify,每日20行。
- 4月7日,RemoveHZ,每日20行。
- 4月8日,DemogetProperties,每日20行。
- 4月9日,GetCurrentTime,每日20行。
- 4月10日,MyThread,每日20行。
- 4月11日,MyThread,每日20行。
- GraphicsLab Project之Parallel Split Shadow Map(PSSM)
- 缓存的基本知识
- 美丽的心形函数
- hive常用语句示例
- Java数组是存储在内存中的什么地方
- 4月19日,TestCommunicate,每日20行。
- Aizu
- HTML5第六课时,布局的简单应用
- postgresql数据库备份和恢复
- Word查找替换功能之快速实现行尾批量加句号
- HTML5第六课时,盒子弧度
- 二叉搜索树与双向链表
- ZOJ 3946 Highway Project(最小费用最短路,优先队列优化dijkstra)
- 转-.NET中GET与SET的用法