jvm - Are Java new objects atomic?
滿天的星座
滿天的星座 2017-06-23 09:14:17
0
4
1197
public static void main(Sting args[]){ Object a=null; new Thread(){ a=new xxx() }.start(); new Thread(){ a=new xxx() }.start(); }

I would like to ask, there is complex object initialization logic in the xxx() method. Is the object created by the new keyword atomic? If not, will there be a problem of object initialization disorder?

滿天的星座
滿天的星座

reply all (4)
扔个三星炸死你

I don’t understand what you mean, if my guess is correct:

It all depends on the specific logic in your construction method. After all, the code is written by humans.

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(); } }

Output:

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

Another example, the constructor contains a synchronization block, and each thread needs to wait for the execution of the previous thread to complete before it can execute.

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(); } }

Output:

2017-06-16:11:49:33:548--begin --从线程Thread-0中创建A 2017-06-16:11:49:35:549--end--从线程Thread-0中创建A A is nwe.Test$A@717c3e10 2017-06-16:11:49:35:550--begin --从线程Thread-1中创建A 2017-06-16:11:49:37:553--end--从线程Thread-1中创建A A is nwe.Test$A@27280786
    淡淡烟草味

    It is recommended to refer to the thread-safe singleton mode

      扔个三星炸死你

      No, for example, if multiple pieces of logic are written in the construction method, it can be interrupted when the construction method is executed.

        洪涛

        The description of "atomicity" is too abstract. When the poster asks questions, it is best not to think that everyone has exactly the same understanding of a certain word. All I can say is that the constructor is thread-safe. For each object, the constructor will only be executed once and by one thread.

          Latest Downloads
          More>
          Web Effects
          Website Source Code
          Website Materials
          Front End Template
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!