1. 程式人生 > >java中的執行緒同步實現方法一(將方法設定為synchronized)

java中的執行緒同步實現方法一(將方法設定為synchronized)

一. 簡要說明:

 對於java中的執行緒同步來說,可以用synchronized關鍵字來修飾,既可以對方法進行修飾,也可以對變數進行修飾,而二者都可以實現執行緒的同步。本篇說的是第一種方法,第二種方法在下一篇中說明。

二. 例子:

    AccountRunnable.java:

public class AccountRunnable implements Runnable {
private static float balance = 500;
private String name;
public AccountRunnable(String name){
this.name = name;
}

public void run(){
try {
while (true) {

deposit(1000);
withdraw(500);
getBalance();
Thread.sleep(300);
}
}
catch(Exception e) {
e.printStackTrace();
}

}
public  synchronized

void getBalance() {

System.out.println("getBalance" + balance);

}
public  synchronized void deposit(float amount) {
System.out.println(name + "deposit " + amount);
balance = balance + amount;

}
public  synchronized void withdraw(float amount) {
System.out.println(name + "withdraw " + amount);
balance = balance - amount;

}
}

ThreadSyncFunTest.java

public class ThreadSyncFunTest {


public static void main(String args[]) {
AccountRunnable r;
Thread t;
ThreadGroup tg = new ThreadGroup("my thread group");
for (int i=0; i<=2; i++) {
r = new AccountRunnable("account " + i);
t = new Thread(tg,r);
t.start();
}
try {
System.in.read();
tg.interrupt();  //採用中斷只為了結束程式的執行
}
catch(Exception e) {
}
        System.out.println("end main");
}

}

測試結果:

--------------------Configuration: <Default>--------------------
account 0 deposit: 1000.0
account 0 withdraw: 500.0
getBalance: 1000.0
account 2 deposit: 1000.0
account 2 withdraw: 500.0
getBalance: 1500.0
account 1 deposit: 1000.0
account 1 withdraw: 500.0
getBalance: 2000.0
account 0 deposit: 1000.0
account 0 withdraw: 500.0
getBalance: 2500.0
account 2 deposit: 1000.0
account 2 withdraw: 500.0
getBalance: 3000.0
account 1 deposit: 1000.0
account 1 withdraw: 500.0
getBalance: 3500.0
account 0 deposit: 1000.0
account 0 withdraw: 500.0
getBalance: 4000.0
account 2 deposit: 1000.0
account 2 withdraw: 500.0
getBalance: 4500.0
account 1 deposit: 1000.0
account 1 withdraw: 500.0
getBalance: 5000.0
account 0 deposit: 1000.0
account 0 withdraw: 500.0
getBalance: 5500.0
account 2 deposit: 1000.0
account 2 withdraw: 500.0
getBalance: 6000.0
account 1 deposit: 1000.0
account 1 withdraw: 500.0
getBalance: 6500.0


end main
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at AccountRunnable.run(AccountRunnable.java:15)
at java.lang.Thread.run(Unknown Source)
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at AccountRunnable.run(AccountRunnable.java:15)
at java.lang.Thread.run(Unknown Source)
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at AccountRunnable.run(AccountRunnable.java:15)
at java.lang.Thread.run(Unknown Source)


Process completed.

四.  說明與分析:

 1.  AccountRunnable類用於模擬3個會計師使用同一個帳戶,各個會計會同時執行存款,提款和查詢的操作,為了得到正確的結果,相關的3個方法必須實現同步化;

      從測試結果仔細分析來看,計算結果都是正確的。

2.  在ThreadSyncFunTest 中,將生成的3個執行緒結合為一組,加入執行緒組,這樣,可同時控制它們;

3.  利用執行緒組的中斷來結束3個執行緒,同時,也意味著結束整個程式,所以,儘管會丟擲異常,但是,這並不會影響整個程式的執行結果。當然,採用這種中斷方式來結束程式的方法有些牽強。