jvm - Java new 對(duì)象是否是原子性的?
問題描述
public static void main(Sting args[]){ Object a=null; new Thread(){ a=new xxx() }.start(); new Thread(){ a=new xxx() }.start();}
想問,xxx()方法里有復(fù)雜的對(duì)象初始化邏輯,new關(guān)鍵字創(chuàng)建對(duì)象,是原子性的嗎?如果不是,會(huì)不會(huì)就出現(xiàn)了對(duì)象初始化錯(cuò)亂的問題?
問題解答
回答1:沒明白你的意思,如果我猜得不錯(cuò)的話:
這完全取決于你的構(gòu)造方法里面的具體的邏輯,畢竟代碼是人寫的。
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() + '中創(chuàng)建A');Thread.sleep(2000);System.out.println(sdf.format(new Date()) + '--end--從線程' + Thread.currentThread().getName() + '中創(chuàng)建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中創(chuàng)建A2017-06-16:11:46:43:780--begin --從線程Thread-0中創(chuàng)建A2017-06-16:11:46:45:786--end--從線程Thread-0中創(chuàng)建A2017-06-16:11:46:45:786--end--從線程Thread-1中創(chuàng)建AA is nwe.Test$A@1e6a629cA is nwe.Test$A@27fcb25d
另一個(gè)例子,構(gòu)造器中包含同步塊,每一個(gè)線程都需要等待前面的線程執(zhí)行完成后才能執(zhí)行。
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() + '中創(chuàng)建A'); Thread.sleep(2000); System.out.println(sdf.format(new Date()) + '--end--從線程' + Thread.currentThread().getName() + '中創(chuàng)建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中創(chuàng)建A2017-06-16:11:49:35:549--end--從線程Thread-0中創(chuàng)建AA is nwe.Test$A@717c3e102017-06-16:11:49:35:550--begin --從線程Thread-1中創(chuàng)建A2017-06-16:11:49:37:553--end--從線程Thread-1中創(chuàng)建AA is nwe.Test$A@27280786回答2:
建議參考線程安全的單例模式
回答3:不具有,比如構(gòu)造方法中寫了多條邏輯,在執(zhí)行構(gòu)造方法時(shí),是可以中斷的。
回答4:“原子性”這種描述太抽象,樓主提問的時(shí)候最好不要認(rèn)為所有人對(duì)某個(gè)詞的認(rèn)識(shí)都完全一樣。我只能說(shuō)構(gòu)造方法是線程安全的,對(duì)于每一個(gè)對(duì)象,構(gòu)造方法只會(huì)被執(zhí)行一次,只會(huì)被一個(gè)線程執(zhí)行。
相關(guān)文章:
1. node.js - mongodb查找子對(duì)象的名稱為某個(gè)值的對(duì)象的方法2. javascript - QQ第三方登錄的問題3. 測(cè)試自動(dòng)化html元素選擇器元素ID或DataAttribute [關(guān)閉]4. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異?!钡膬?nèi)存錯(cuò)誤?5. spring-mvc - spring-session-redis HttpSessionListener失效6. 在mac下出現(xiàn)了兩個(gè)docker環(huán)境7. 利用IPMI遠(yuǎn)程安裝centos報(bào)錯(cuò)!8. mysql - 查詢 修改數(shù)據(jù)庫(kù)優(yōu)化問題吧9. 正在使用electron和node.js做桌面應(yīng)用,需要實(shí)時(shí)監(jiān)聽是否有網(wǎng)絡(luò)連接,node或者electron是否可以做到10. java - Spring boot 讀取 放在 jar 包外的,log4j 配置文件,系統(tǒng)有創(chuàng)建日志文件,不寫入日志信息。

網(wǎng)公網(wǎng)安備