我们是利用synchronized的两种方法:声明一条语句或声明一个方法
方法——:同步语句,使用synchronized(对象)后面接语句即可
方法二 同步方法,使用synchronized(this){ 方法体 }
附方法一代码如下:
package AcountThread;
public class Account {
String name;
private int balance;
public String getName() {
return name;
}
public int getBalance() {
return balance;
}
public Account(String name) {
this.name = name;
this.balance = 0;
}
// 存钱
public void put(int value) { // value为存入金额
if (value > 0) {
this.balance += value;
}
}
// 取钱方法
public double get(int value) {
if (value > 0) {
if (value < this.balance) {
this.balance -= value;
} else {
value = this.balance; // 取走所有余额
this.balance = 0;
}
}
return value;
}
}
package AcountThread;
public class FecthThread extends Thread {
public Account account;
public int value;
public FecthThread(Account account, int value) {
this.account = account;
this.value = value;
}
public void run() {
synchronized (account) { //拿锁对象
// 查看余额
double howmatch = this.account.getBalance();
// 取钱
double v = this.account.get(value);
// 查看余额
System.out.println(this.account.name + "账户:现有" + howmatch + ",取走"
+ v + ",余额" + this.account.getBalance());
} // 解锁对象
}
}
package AcountThread;
public class SevaThread extends Thread {
public Account account;
public int value;
public SevaThread(Account account, int value) {
this.account = account;
this.value = value;
}
public void run() {
synchronized (account) {
// 查看
double howmatch = this.account.getBalance();
// 存钱
this.account.put(value);
// 查看
System.out.println(account.getName() + "账户:现有" + howmatch + ",存入"
+ this.value + ",余额" + this.account.getBalance());
;
}
}
}
package AcountThread;
public class MainThread {
public static void main(String[] args) {
Account wang = new Account("wang");
(new SevaThread(wang, 100)).start();// 匿名对象等价于SevaThred t1 = new
// SevaThread(wang,
// 100);t1.start();
(new SevaThread(wang, 200)).start();
(new FecthThread(wang, 300)).start();
}
}
附方法一代码如下:package AcountThread.v2;
public class Account {
private String name;
private int balance;
public String getName() {
return name;
}
public int getBalance() {
return balance;
}
public Account(String name) {
this.name = name;
this.balance = 0;
}
// 存钱方法
synchronized public void put(int value) { // value为存入金额
if (value > 0) {
double howmatch = balance; // 查看起初的金额
this.balance += value;
System.out.println(name + "账户:现有" + howmatch + ",存入" + value
+ ",余额" + balance);
;
}
}
// 取钱方法
synchronized public void get(int value) {
int howmatch = balance; // 查看余额
if (value > 0) {
if (value < this.balance) {
this.balance -= value; //
} else {
value = this.balance; // 取走所有余额
this.balance = 0;
}
}
System.out.println(name + "账户:现有" + howmatch + ",取走" + value + ",余额"
+ balance);
}
}
package AcountThread.v2;
public class FecthThread extends Thread {
public Account account;
public int value;
public FecthThread(Account account, int value) {
this.account = account;
this.value = value;
}
public void run() {
// 取钱
this.account.get(value);
}
}
package AcountThread.v2;
public class SevaThread extends Thread {
public Account account;
public int value;
public SevaThread(Account account, int value) {
this.account = account;
this.value = value;
}
public void run() {
// 存钱
this.account.put(value);
}
}
package AcountThread.v2;
public class MainThread {
public static void main(String[] args) {
Account wang = new Account("wang");
(new SevaThread(wang, 100)).start(); // 匿名对象等价于SevaThred t1 = new
// SevaThread(wang,
// 100);t1.start();
(new SevaThread(wang, 200)).start();
(new FecthThread(wang, 300)).start();
}
}