search

Home  >  Q&A  >  body text

jvm - Are Java new objects atomic?

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?

滿天的星座滿天的星座2712 days ago1263

reply all(4)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-06-23 09:16:17

    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
    

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-23 09:16:17

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

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-06-23 09:16:17

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

    reply
    0
  • PHP中文网

    PHP中文网2017-06-23 09:16:17

    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.

    reply
    0
  • Cancelreply