2

In below i'm trying to send two threads in singleton Pattern to see how the two threads working without synchronizing.In theory two threads should make two separate objects.But here without Synchronizing,only one object is creating and details of two threads are assigning two that object.I can see it when i print the object.How is that happening.why aren't there two objects are creating?

public class Singleton_Pattern {

    public static void main(String[] args) {
     Test1 t1=new Test1();
     t1.start();
     Test2 t2=new Test2();
     t2.start();
     t1.m();
     t2.m();
    }
}

class Test1 extends Thread {

    void m() {
        System.out.println(A.getA());
        A.getA().setValue("Cat");
        System.out.println(A.getA().getValue());
    }
}

class Test2 extends Thread {

    void m() {
        System.out.println(A.getA());
        A.getA().setValue("Dog");
        System.out.println(A.getA().getValue());
    }
}

class A {

    private static A a;
    String name;

    private A() {

    }

    public static A getA() {
        if (a == null) {

            a = new A();
        }
        return a;
    }

    public void setValue(String t) {
        name = t;
    }

    public String getValue() {
        return name;
    }
}
  • Since your singleton `A` is not thread-safe, results are unpredictable. – Andreas Jan 17 '19 at 05:37
  • 1
    you don't have a `run()` method in your thread classes.. I think you are calling `m()` on the main thread – Kartik Jan 17 '19 at 05:39
  • You make a singleton, and then ask *"why aren't there two objects are creating?"*???? Do you understand what **singleton** means? It mean that there is **only one**, so why would you expect two of them? – Andreas Jan 17 '19 at 05:39
  • @Andreas OP would expect that because without synchronization, multiple threads can enter `if (a == null)` at the same time – Kartik Jan 17 '19 at 05:41
  • @Kartik: That would be possible, but pretty improbable. I would certainly not be "expecting it", though I would still want to defend against the possibility. If OP wanted one singleton per thread, they should be using `ThreadLocal`. – Amadan Jan 17 '19 at 05:43
  • @Amadan I agree.. my comment was in response to Andreas's comment - " *Do you understand what singleton means? It mean that there is only one, so why would you expect two of them?* " – Kartik Jan 17 '19 at 05:45
  • @Kartik How does "multiple threads can enter at the same time" mean that static field `a` would even be able to refer to more than one object at a time? – Andreas Jan 17 '19 at 05:47
  • @Andreas I know, I was just trying to clarify what the OP might be thinking. Anyway, ignore me. :P – Kartik Jan 17 '19 at 05:51

2 Answers2

0

Why do you think that calling getA() from different threads should create different objects? An application has only a single static area. So in this case both threads shall use the same object.

And in your example you are calling the m() method using the main thread. You should override run() method to use multiple threads effectively. Add the following method in your Test1 and Test2 classes.

public void run() {
   m();
}

and remove the following lines from your main method,

t1.m();
t2.m();
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
0

You are calling m() on the main thread. To call it in a separate thread, you need to override run() method in your thread classes and call m() from within run().

Once you fix this, it is very improbable that both the threads enter the block exactly at the same time, down to nanoseconds. You can try having many threads and running loops to see concurrency issues. But because a is static, the "new" object would quickly overwrite the old instance.

Have a look at this answer to understand synchronisation in singleton pattern.

Kartik
  • 7,677
  • 4
  • 28
  • 50