您的位置:首页 > 编程语言 > Java开发

java的线程的同步安全分析

2014-06-06 15:17 567 查看
public class Threadsychn {

public static void main(String[] args) {

// TODO 自动生成的方法存根

final Foo f=new Foo();

Thread t=new Thread(){

public void run(){

f.add();

}

};

t.start();

f.add(2);

}

}

class Foo{

int a=0;

public synchronized void add(int b){

System.out.println("call add(b)");

try{

Thread.sleep(100);

}catch(InterruptedException e){

e.printStackTrace();

}

a+=b;

System.out.println("over call add(b)");

}

public synchronized void add(){

System.out.println("call add()");

try{

Thread.sleep(100);

}catch(InterruptedException e){

e.printStackTrace();

}

a++;

System.out.println("over call add()");

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: