关于线程安全的一些小问题

 Person 类代码如下

public class Person {
    private int age;
    
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


}

 

public class Easy {

    private Person person = new Person();

    private static volatile Easy easy = null;

    private Easy() {
    }


    public static Easy instance() {
        if (easy == null) {
            synchronized (Easy.class) {
                if (easy == null) {
                    easy = new Easy();
                }
            }
        }
        return easy;
    }


    public void printPerson() {
        System.out.println(person);
    }


    public static void main(String[] args) throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Easy.instance().printPerson();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Easy.instance().printPerson();
            }
        }).start();
    }
}

运行结果:

com.example.task.Person@6ab31cd3
com.example.task.Person@6ab31cd3

典型的火车买票的代码,这里这个person是线程共享的,存在堆内存中。

 

 

public class Easy {

    private Person person = null;

    private static volatile Easy easy = null;

    private Easy() {
    }

    public static Easy instance() {
        if (easy == null) {
            synchronized (Easy.class) {
                if (easy == null) {
                    easy = new Easy();
                }
            }
        }
        return easy;
    }


    public void printPerson() {
        System.out.println(person);
        person = new Person();
        System.out.println(person);
    }


    public static void main(String[] args) throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
               Easy.instance().printPerson();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Easy.instance().printPerson();
            }
        }).start();
    }
}

 运行结果:

null
com.example.task.Person@28979470
com.example.task.Person@28979470
com.example.task.Person@26507f12

 

从这里看到,如果在线程一中对成员变量进行赋值,切换到线程二的时候引用还是线程一的。

 

 

对于下面这段代码,为什么永远是同一个对象呢

 @Autowired
 private DialogueManager dialogueManager;

 这是因为工厂(spring工厂)获取的永远是用一个对象,使用代码可以这样表示

 

 

package com.example.task;

public class Person {
    private static volatile Person person = null;

    private Person() {
    }

    public static Person instance() {
        if (person == null) {
            synchronized (Person.class) {
                if (person == null) {
                    person = new Person();
                }
            }
        }
        return person;
    }

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private int age;
}

 

package com.example.task;


public class Easy {

    private Person person = null;

    private static volatile Easy easy = null;

    private Easy() {
    }


    public static Easy instance() {
        if (easy == null) {
            synchronized (Easy.class) {
                if (easy == null) {
                    easy = new Easy();
                }
            }
        }
        return easy;
    }


    public void printPerson() {
        System.out.println(person);
        person = Person.instance();
        System.out.println(person);
    }


    public static void main(String[] args) throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
               Easy.instance().printPerson();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Easy.instance().printPerson();
            }
        }).start();
    }
}

 运行结果:

 

null
com.example.task.Person@4f114b68
com.example.task.Person@4f114b68
com.example.task.Person@4f114b68

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值