本帖最后由 王浩龙 于 2014-3-13 08:02 编辑
- <p>
- class Bank{
- private int sum;//sum没有初始化值为什么可以编译成功呢?
- public synchronized void add(int n){
- sum = sum+n;
- System.out.println("sum = "+sum);
- }
-
- }
- class Custom implements Runnable{
- private Bank bank = new Bank();
- public void run(){
- for(int i=0; i<3;i++){
- bank.add(100);
- }
- }
- }
- public class BankDemo {
- public static void main(String[] args) {
- Custom cus = new Custom();
- Thread t1 = new Thread(cus);
- Thread t2 = new Thread(cus);
- t1.start();
- t2.start();
- }
- }</p><div class="blockcode"><blockquote>
- <p> </p>
复制代码 这里的sum是因为初始化为0吗?
|