java中的线程同步实现方法一(将方法设置为synchronized)

来源:互联网 发布:歼20就是个笑话 知乎 编辑:程序博客网 时间:2024/06/10 20:57

一. 简要说明:

 对于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个线程,同时,也意味着结束整个程序,所以,尽管会抛出异常,但是,这并不会影响整个程序的执行结果。当然,采用这种中断方式来结束程序的方法有些牵强。


原创粉丝点击