java同步通信方式_java多线程同步与通信示例(synchronized方式)
java多線程同步示例,來自《瘋狂java講義》。通過synchronized,wait(),notify(),notifyAll()實現多線程同步與通信。假設現在系統中有兩個線程,這兩個線程分別代表存錢者和取錢者---現在假設系統有一種特殊的要求,系統要求存款者和取錢者不斷地重復存錢、取錢動作,而且要求每當存款著將錢存入指定賬戶后,取錢者就立即取出這筆錢。不允許存款者連續兩次存錢,也不允許取錢者連續兩次取錢。程序中,通過一個旗標flag標識,本次賬戶操作是不是存款操作,以便下次操作可以探測。可類比:http://www.oschina.net/code/snippet_820500_44795
1.[代碼]Account賬戶類,有取錢(drawBalance)和存錢(deposit)兩個同步方法
package com.sunchp;
public class Account {
private String accountNo;
private double balance;
private boolean flag = false;
public Account(String _accountNo, double _balance) {
this.accountNo = _accountNo;
this.balance = _balance;
}
public String getAccountNo() {
return accountNo;
}
public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}
public double getBalance() {
return balance;
}
public synchronized void drawBalance(double drawBalance) {
if (flag) {
if (balance > drawBalance) {
System.out.println(Thread.currentThread().getName() + "取錢成功,正在吐鈔:" + drawBalance);
balance -= drawBalance;
flag = false;
this.notifyAll();
System.out.println(Thread.currentThread().getName() + "余額為:" + getBalance());
} else {
System.out.println(Thread.currentThread().getName() + "取錢失敗,余額不足");
}
} else {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void deposit(double depositBalance) {
if (flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println(Thread.currentThread().getName() + " 存款:" + depositBalance);
this.balance += depositBalance;
flag = true;
this.notifyAll();
System.out.println(Thread.currentThread().getName() + "余額為:" + getBalance());
}
}
}
2.[代碼]取錢線程類
package com.sunchp;
public class MyThread1 implements Runnable {
private Account account;
private double drawBalance;
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public double getDrawBalance() {
return drawBalance;
}
public void setDrawBalance(double drawBalance) {
this.drawBalance = drawBalance;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
account.drawBalance(drawBalance);
}
}
}
3.[代碼]存錢線程類
package com.sunchp;
public class MyThread2 implements Runnable {
private Account account;
private double depositBalance;
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public double getDepositBalance() {
return depositBalance;
}
public void setDepositBalance(double depositBalance) {
this.depositBalance = depositBalance;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
account.deposit(depositBalance);
}
}
}
4.[代碼]測試類,main方法
package com.sunchp;
public class Test {
public static void main(String[] args) throws Exception {
Account acc = new Account("001", 1000);
MyThread1 mt1 = new MyThread1();
mt1.setAccount(acc);
mt1.setDrawBalance(20);
Thread t1 = new Thread(mt1, "取錢者1");
Thread t2 = new Thread(mt1, "取錢者2");
MyThread2 mt2 = new MyThread2();
mt2.setAccount(acc);
mt2.setDepositBalance(20);
Thread t3 = new Thread(mt2, "存錢者1");
Thread t4 = new Thread(mt2, "存錢者2");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
總結
以上是生活随笔為你收集整理的java同步通信方式_java多线程同步与通信示例(synchronized方式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 煅浮石的功效与作用、禁忌和食用方法
- 下一篇: 红海参的功效与作用、禁忌和食用方法