- class Bank{
- private int sum;
- public synchronized void add(int n){
- sum = sum+n;
- System.out.println("sum="+sum);
- }
- }
- class Cus implements Runnable{
- private Bank b = new Bank();
- public void run(){
- for(int x=0;x<3;x++){
- b.add(100);
- }
- }
- }
- class BankDemo{
- public static void main(){
- Cus c = new Cus();
- Thread t = new Thread(c);
- Thread t1 = new Thread(c);
- t1.start();
- t.start();
- }
- }
复制代码 老毕说:“ 操作共享数据 b 这个只有一句 不能被别人分开读 ” 这句不理解啊 为啥b 不能被别的线程分开读??? 求高人解答 |
|