jvm - Java new 對(duì)象是否是原子性的?
問(wèn)題描述
public static void main(Sting args[]){ Object a=null; new Thread(){ a=new xxx() }.start(); new Thread(){ a=new xxx() }.start();}
想問(wèn),xxx()方法里有復(fù)雜的對(duì)象初始化邏輯,new關(guān)鍵字創(chuàng)建對(duì)象,是原子性的嗎?如果不是,會(huì)不會(huì)就出現(xiàn)了對(duì)象初始化錯(cuò)亂的問(wèn)題?
問(wèn)題解答
回答1:沒(méi)明白你的意思,如果我猜得不錯(cuò)的話:
這完全取決于你的構(gòu)造方法里面的具體的邏輯,畢竟代碼是人寫(xiě)的。
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)造方法中寫(xiě)了多條邏輯,在執(zhí)行構(gòu)造方法時(shí),是可以中斷的。
回答4:“原子性”這種描述太抽象,樓主提問(wèn)的時(shí)候最好不要認(rèn)為所有人對(duì)某個(gè)詞的認(rèn)識(shí)都完全一樣。我只能說(shuō)構(gòu)造方法是線程安全的,對(duì)于每一個(gè)對(duì)象,構(gòu)造方法只會(huì)被執(zhí)行一次,只會(huì)被一個(gè)線程執(zhí)行。
相關(guān)文章:
1. mysql - 如何減少使用或者不用LEFT JOIN查詢?2. html5 - H5 audio 微信端 在IOS上不能播放音樂(lè)3. python - 編碼問(wèn)題求助4. python - 我在使用pip install -r requirements.txt下載時(shí),為什么部分能下載,部分不能下載5. 視頻文件不能播放,怎么辦?6. mysql - 分庫(kù)分表、分區(qū)、讀寫(xiě)分離 這些都是用在什么場(chǎng)景下 ,會(huì)帶來(lái)哪些效率或者其他方面的好處7. mysql - jdbc的問(wèn)題8. python - Scrapy存在內(nèi)存泄漏的問(wèn)題。9. Python爬蟲(chóng)如何爬取span和span中間的內(nèi)容并分別存入字典里?10. mysql - 千萬(wàn)級(jí)數(shù)據(jù)的表,添加unique約束,insert會(huì)不會(huì)很慢?
