关于线程同步问题
来源:互联网 发布:三维彩超数据透漏男女 编辑:程序博客网 时间:2024/05/11 18:36
class SalesLady {
int memontoes, five, ten;
public synchronized String ruleForSale(int num, int money) {
String s = null;
if (memontoes == 0)
return "对不起,已经售完";
if (money == 5) {
memontoes--;
five++;
s = "给你票," + "你的钱正好。";
} else if (money == 10) {
while (five < 1) {
try {
System.out.println("" + num + "号顾客用10元购票,请等待");
wait();
} catch (InterruptedException e) {
}
}
memontoes--;
five -= 1;
ten++;
s = "给你票," + "找你5元。";
}
notifyAll();
return s;
}
SalesLady(int m, int f, int t) {
memontoes = m;
five = f;
ten = t;
}
}
public class 八_5 extends java.applet.Applet {
static SalesLady saleslady = new SalesLady(14, 0, 0);
public void start() {
int moneies[] = { 10, 10, 5, 10, 5, 10, 5, 5, 10, 5, 10, 5, 5, 10, 5};
Thread[] aThreadArray = new Thread[20];
System.out.println("现在开始售票:");
for (int i = 0; i < moneies.length; i++) {
aThreadArray[i] = new Thread(new CustomerClass(i + 1, moneies[i])); aThreadArray[i].start(); } WhileLoop: while (true) {
for (int i = 0; i < moneies.length; i++)
if (aThreadArray[i].isAlive())
continue WhileLoop;
break;
}
System.out.println("票已售完");
}
}
class CustomerClass implements Runnable {
int num, money;
public void run() {
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
}
System.out.println("我是" + num + "号顾客,用" + money + "元购票,售票员说:"
+ 八_5.saleslady.ruleForSale(num, money));
}
CustomerClass(int n, int m) {
num = n;
money = m;
}
}
int memontoes, five, ten;
public synchronized String ruleForSale(int num, int money) {
String s = null;
if (memontoes == 0)
return "对不起,已经售完";
if (money == 5) {
memontoes--;
five++;
s = "给你票," + "你的钱正好。";
} else if (money == 10) {
while (five < 1) {
try {
System.out.println("" + num + "号顾客用10元购票,请等待");
wait();
} catch (InterruptedException e) {
}
}
memontoes--;
five -= 1;
ten++;
s = "给你票," + "找你5元。";
}
notifyAll();
return s;
}
SalesLady(int m, int f, int t) {
memontoes = m;
five = f;
ten = t;
}
}
public class 八_5 extends java.applet.Applet {
static SalesLady saleslady = new SalesLady(14, 0, 0);
public void start() {
int moneies[] = { 10, 10, 5, 10, 5, 10, 5, 5, 10, 5, 10, 5, 5, 10, 5};
Thread[] aThreadArray = new Thread[20];
System.out.println("现在开始售票:");
for (int i = 0; i < moneies.length; i++) {
aThreadArray[i] = new Thread(new CustomerClass(i + 1, moneies[i])); aThreadArray[i].start(); } WhileLoop: while (true) {
for (int i = 0; i < moneies.length; i++)
if (aThreadArray[i].isAlive())
continue WhileLoop;
break;
}
System.out.println("票已售完");
}
}
class CustomerClass implements Runnable {
int num, money;
public void run() {
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
}
System.out.println("我是" + num + "号顾客,用" + money + "元购票,售票员说:"
+ 八_5.saleslady.ruleForSale(num, money));
}
CustomerClass(int n, int m) {
num = n;
money = m;
}
}
- 关于线程同步问题
- 关于线程同步问题
- 关于Java线程同步问题
- 关于线程和进程的同步问题
- 关于java中的线程同步问题
- 关于线程和进程的同步问题
- Java 关于线程同步的问题
- 关于Java中线程同步的问题
- 关于Java的线程同步问题
- 关于Java中线程同步问题
- java Android 中关于线程同步问题
- 关于事务并发、线程同步概念问题
- java线程系列---关于线程同步与互斥问题
- 关于mfc线程 的退出问题、同步问题
- 关于线程同步
- 关于线程同步 PulseEvent()
- 关于线程同步(Lock)
- java 关于线程同步
- Delphi代码标准文档
- 10.30
- tomcat 的时区设置问题
- JS中使用COM访问Lotus组件(续)
- 残品...
- 关于线程同步问题
- The function default must be used with a prefix when a default namespace is not specified
- 用mutex同步多线程
- 要去度假了!
- 标准日本语(初级)课堂课程学习结束
- IIS性能设置
- VC++得到当前系统时间日期 GetSystemTime()
- ubuntu中安装配置Apache Tomcat(三)
- FFT结果的物理意义