学习多线程之前,我觉得很有必要去学习下
[笔记][思维导图]读深入理解JAVA内存模型整理的思维导图
其他三点,请看java内存模型
例子:
public class SyncStatic {
public static void main(String[] args) throws InterruptedException {
public class SyncStatic {
public static void main(String[] args) throws InterruptedException {
final A a = new A();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
a.addStatic();
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
a.sub();
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t2.start();
t1.join();
t2.join();
System.out.println(a.num);
}
}
class A{
public static int num = 0;
public static synchronized void addStatic(){
++num;
System.out.println("addStatic:" + num);
}
/**
* 想看到正确的结果,请把这里的static注释放开
*/
public /*static*/ synchronized void sub(){
--num;
System.out.println("sub:" + num);
}
}
某一次运行结果:
addStatic:0
sub:0
sub:0
addStatic:1
addStatic:1
sub:0
sub:-1
addStatic:0
sub:-1
addStatic:0
addStatic:1
sub:1
addStatic:1
sub:1
sub:0
addStatic:1
sub:1
addStatic:1
addStatic:2
sub:2
2
说明:
一个A类。两个线程:一个线程相加10次,一个线程相减10次,最后的结果肯定是不变的。但是这里实现正面变化了。所以说:要注意被syncronized修饰过的静态方法
**
* Created by zhuqiang on 2015/8/5 0005.
* 模拟账户
*/
public class Account {
private String userName; //用户
private double money; //余额
public synchronized void add(double money){
this.money = this.money + money;
System.out.println(this.getUserName() + ",+100:余额:" + this.getMoney());
}
public synchronized void subtract(double money){
this.money = this.money - money;
System.out.println(this.getUserName() + ",-100:余额:" + this.getMoney());
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
/**
* Created by zhuqiang on 2015/8/5 0005.
* atm 机器,往里面不断的充值
*/
public class AtmAdd implements Runnable {
private Account account;
public AtmAdd(Account account){
this.account = account;
}
@Override
public void run() {
for (int i = 1;i <=100;i++){
account.add(100);
}
}
}
/**
* Created by zhuqiang on 2015/8/5 0005.
* atm 机器,往里面不断的扣钱
*/
public class AtmSub implements Runnable {
private Account account;
public AtmSub(Account account){
this.account = account;
}
@Override
public void run() {
for (int i = 1;i <=100;i++){
account.subtract(100);
}
}
}
/**
* Created by zhuqiang on 2015/8/5 0005.
*/
public class Client {
public static void main(String[] args) throws Exception {
Account account = new Account();
account.setMoney(0);
account.setUserName("小明");
Thread add = new Thread(new AtmAdd(account));
add.start();
Thread sub = new Thread(new AtmSub(account));
sub.start();
add.join(); //等待该线程终止,主线程才执行
sub.join();
System.out.println(account.getMoney());
}
}
说明:
运行结果就不贴出了;
该示例有2个线程,一个线程不断的往账户中冲钱,一个线程不断的往账户中扣钱。正确同步的结果是最后账户中的钱是不变的。因为执行的次数都一样
版权声明:本文为博主原创文章,未经博主允许不得转载。
[笔记][Java7并发编程实战手册]2.2使用syncronized实现同步方法
原文地址:http://blog.csdn.net/mr_zhuqiang/article/details/47356927