多线程运行正常但没有达到自己那种想无限循环下去的效果,不信可以自己拷过去试下,几秒钟就停住了
public class TestThread {
public static void main(String[] args) {
Parent p1 = new Parent("爸爸",1000);
Parent p2 = new Parent("妈妈",1000);
Child c1 = new Child("孩子1",300);
Child c2 = new Child("孩子2",500);
Thread t1 = new Thread(p1);
Thread t2 = new Thread(p2);
Thread t3 = new Thread(c1);
Thread t4 = new Thread(c2);
t1.start();t2.start();t3.start();t4.start();
}
}
class BankCard {
int sum=0;
public synchronized void save(String s,int count) throws Exception{
while(sum > 3000) {
System.out.println(s+"你等等,已经有足够存款了");
this.wait();
}
this.sum += count;
notifyAll();
System.out.println(s+"村了"+count+"块钱,孩子们,快来取用吧");
}
public synchronized void disposit(String s,int count) throws Exception {
while(sum < count) {
System.out.println(s+"你等等,钱不够了");
this.wait();
}
this.sum -= count;
notifyAll();
System.out.println(s+"取出了"+count+"块钱");
}
}
class Parent implements Runnable {
String s; int count; BankCard b = new BankCard();
Parent(String s,int count) {
this.s = s;
this.count = count;
}
public void run() {
while(true) {
try{
b.save(s,count);
Thread.sleep(2000);
}
catch(Exception e) {
}
}
}
}
class Child implements Runnable {
String s; int count; BankCard b = new BankCard();
Child(String s,int count) {
this.s = s;
this.count = count;
}
public void run() {
while(true) {
try {
b.disposit(s,count);
Thread.sleep(2000);
}
catch(Exception e) {
}
}
}
}
public class TestThread {
public static void main(String[] args) {
Parent p1 = new Parent("爸爸",1000);
Parent p2 = new Parent("妈妈",1000);
Child c1 = new Child("孩子1",300);
Child c2 = new Child("孩子2",500);
Thread t1 = new Thread(p1);
Thread t2 = new Thread(p2);
Thread t3 = new Thread(c1);
Thread t4 = new Thread(c2);
t1.start();t2.start();t3.start();t4.start();
}
}
class BankCard {
int sum=0;
public synchronized void save(String s,int count) throws Exception{
while(sum > 3000) {
System.out.println(s+"你等等,已经有足够存款了");
this.wait();
}
this.sum += count;
notifyAll();
System.out.println(s+"村了"+count+"块钱,孩子们,快来取用吧");
}
public synchronized void disposit(String s,int count) throws Exception {
while(sum < count) {
System.out.println(s+"你等等,钱不够了");
this.wait();
}
this.sum -= count;
notifyAll();
System.out.println(s+"取出了"+count+"块钱");
}
}
class Parent implements Runnable {
String s; int count; BankCard b = new BankCard();
Parent(String s,int count) {
this.s = s;
this.count = count;
}
public void run() {
while(true) {
try{
b.save(s,count);
Thread.sleep(2000);
}
catch(Exception e) {
}
}
}
}
class Child implements Runnable {
String s; int count; BankCard b = new BankCard();
Child(String s,int count) {
this.s = s;
this.count = count;
}
public void run() {
while(true) {
try {
b.disposit(s,count);
Thread.sleep(2000);
}
catch(Exception e) {
}
}
}
}
解决方案 »
- 请教各位大神批量添加的问题
- EJB应用为什么不能被同一个项目中的test.jsp文件调用?
- 权限控制
- hibernate Util类的写法
- 看看我的Hibernate错误信息,帮我分析一下吧?
- 十万火急!!为什么netbeens 无法安装在 xp操作系统上?
- JAVA应用程序开发的困惑
- tomcat下一个简单的jsp例子,errorPage出不来
- 小弟初学struts(IDE JB),做了个最简单的jsp文件,对应一个ActionForm和一个action,但是编译不过去?
- 请问在tomcat下可以部署ejb吗?怎样部署?谢谢!
- HashMap换成FastMap 相对会比较好吗?--菜鸟提问
- ognl valueStack
执行结果:
爸爸村了1000块钱,孩子们,快来取用吧
孩子1你等等,钱不够了
妈妈村了1000块钱,孩子们,快来取用吧
孩子2你等等,钱不够了
爸爸村了1000块钱,孩子们,快来取用吧
妈妈村了1000块钱,孩子们,快来取用吧
爸爸村了1000块钱,孩子们,快来取用吧
妈妈村了1000块钱,孩子们,快来取用吧
爸爸村了1000块钱,孩子们,快来取用吧
妈妈村了1000块钱,孩子们,快来取用吧
爸爸你等等,已经有足够存款了
妈妈你等等,已经有足够存款了
下面就不动了,寻高人讲解
thank you
BankCard b = new BankCard();
这4个线程不是共享一份数据,而是各自有着自己的数据。所以他们都是对自己的数据进行处理,并没有数据共享。这样父母亲存满自己的那份钱就wait了,孩子取完自己的钱就wait了
我讲的不清楚,贴一下我改的代码.public class TestThread {
public static void main(String[] args) {
BankCard b = new BankCard();//4个线程共享数据,将这个对象传给4个线程,而不是在线程中new
Parent p1 = new Parent("爸爸",1000,b);
Parent p2 = new Parent("妈妈",1000,b);
Child c1 = new Child("孩子1",300,b);
Child c2 = new Child("孩子2",500,b);
Thread t1 = new Thread(p1);
Thread t2 = new Thread(p2);
Thread t3 = new Thread(c1);
Thread t4 = new Thread(c2);
t1.start();t2.start();t3.start();t4.start();
}
}
class BankCard {
int sum=0;
public synchronized void save(String s,int count) throws Exception{
while(sum > 3000) {
System.out.println(s+"你等等,已经有足够存款了");
this.wait();
}
this.sum += count;
notifyAll();
System.out.println(s+"村了"+count+"块钱,孩子们,快来取用吧");
}
public synchronized void disposit(String s,int count) throws Exception {
while(sum < count) {
System.out.println(s+"你等等,钱不够了");
this.wait();
}
this.sum -= count;
notifyAll();
System.out.println(s+"取出了"+count+"块钱");
}
}
class Parent implements Runnable {
String s; int count; BankCard b;
Parent(String s,int count,BankCard b) {
this.s = s;
this.count = count;
this.b = b;
}
public void run() {
while(true) {
try{
b.save(s,count);
Thread.sleep(2000);
}
catch(Exception e) {
}
}
}
}
class Child implements Runnable {
String s; int count; BankCard b;
Child(String s,int count,BankCard b) {
this.s = s;
this.count = count;
this.b = b;
}
public void run() {
while(true) {
try {
b.disposit(s,count);
Thread.sleep(2000);
}
catch(Exception e) {
}
}
}
}