spring单例是线程安全的吗?

我写一段代码来验证:
public class Singleton {
int m=-1;
private Singleton() {
m=0;
}

private static Singleton instance = new Singleton();

public static Singleton getInstance() {
return instance;
}
public static void main(String[] args){
Singleton single=Singleton.getInstance();
NewThread t1=new NewThread();
NewThread t2=new NewThread();
NewThread t3=new NewThread();
t1.setSingle(single);
t2.setSingle(single);
t3.setSingle(single);
t1.start();
t2.start();
t3.start();
}
}
class NewThread extends Thread{
private Singleton single;
public void setSingle(Singleton single) {
this.single = single;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
for(int i=1;i<10;i++){
single.m=i;
System.out.println("current thread:"+Thread.currentThread()+"current m:"+single.m);
}
}

}

通过输出的结果来是:每个线程都输出了0~9,应该是线程安全的,但我不知道我这个测试逻辑上是否是正确的, :oops: ,谁能给个详细的解答?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值