您的位置:首页技术文章
文章详情页

jvm - Java new 对象是否是原子性的?

浏览:60日期:2023-10-26 16:18:59

问题描述

public static void main(Sting args[]){ Object a=null; new Thread(){ a=new xxx() }.start(); new Thread(){ a=new xxx() }.start();}

想问,xxx()方法里有复杂的对象初始化逻辑,new关键字创建对象,是原子性的吗?如果不是,会不会就出现了对象初始化错乱的问题?

问题解答

回答1:

没明白你的意思,如果我猜得不错的话:

这完全取决于你的构造方法里面的具体的逻辑,毕竟代码是人写的。

public class Test { static class A{public A(){ try {SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd:hh:mm:ss:SS');System.out.println(sdf.format(new Date()) + '--begin --从线程' + Thread.currentThread().getName() + '中创建A');Thread.sleep(2000);System.out.println(sdf.format(new Date()) + '--end--从线程' + Thread.currentThread().getName() + '中创建A'); } catch (InterruptedException e) {e.printStackTrace(); }} }public static void main(String[] args) {new Thread(new Runnable(){ @Override public void run() {System.out.println('A is ' +new A()); } }).start();new Thread(new Runnable(){ @Override public void run() {System.out.println('A is ' +new A()); } }).start(); }}

输出:

2017-06-16:11:46:43:780--begin --从线程Thread-1中创建A2017-06-16:11:46:43:780--begin --从线程Thread-0中创建A2017-06-16:11:46:45:786--end--从线程Thread-0中创建A2017-06-16:11:46:45:786--end--从线程Thread-1中创建AA is nwe.Test$A@1e6a629cA is nwe.Test$A@27fcb25d

另一个例子,构造器中包含同步块,每一个线程都需要等待前面的线程执行完成后才能执行。

import java.text.*;import java.util.Date;public class Test { static class A{public A(){ try {synchronized (Test.class) { SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd:hh:mm:ss:SS'); System.out.println(sdf.format(new Date()) + '--begin --从线程' + Thread.currentThread().getName() + '中创建A'); Thread.sleep(2000); System.out.println(sdf.format(new Date()) + '--end--从线程' + Thread.currentThread().getName() + '中创建A');} } catch (InterruptedException e) {e.printStackTrace(); }} }public static void main(String[] args) {new Thread(new Runnable(){ @Override public void run() {System.out.println('A is ' +new A()); } }).start();new Thread(new Runnable(){ @Override public void run() {System.out.println('A is ' +new A()); } }).start(); }}

输出:

2017-06-16:11:49:33:548--begin --从线程Thread-0中创建A2017-06-16:11:49:35:549--end--从线程Thread-0中创建AA is nwe.Test$A@717c3e102017-06-16:11:49:35:550--begin --从线程Thread-1中创建A2017-06-16:11:49:37:553--end--从线程Thread-1中创建AA is nwe.Test$A@27280786回答2:

建议参考线程安全的单例模式

回答3:

不具有,比如构造方法中写了多条逻辑,在执行构造方法时,是可以中断的。

回答4:

“原子性”这种描述太抽象,楼主提问的时候最好不要认为所有人对某个词的认识都完全一样。我只能说构造方法是线程安全的,对于每一个对象,构造方法只会被执行一次,只会被一个线程执行。

标签: java
相关文章: