• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton is behaving different with synchronized method  RSS feed

 
amithk kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a main class and ran a thread for 10000 tines each time it hits run method it will call l SingleTon.getInstance method and adds hashcode to a list but i am wondering why iam getting

output like this even though the getinstance method of SingleTon is synchronized

[1388889817, 1388889817, 1388889817, 1388889817, 1388889817, 1388889817, 1388889817, 1388889817, 1388889817, 1388889817, null, ...so on

here the unique hashcode which iam able to see
[null, 1388889817] why null is there only the hashcode 1388889817 should be present.


public class InsSingleton implements Runnable {

public List<Integer> list = new ArrayList<>();
public int cnt = 0;


// Adding hashcode values to a list
@Override
public void run() {

list.add(SingleTon.getInstance().hashCode());

}

}


public static void main(String[] args) throws InterruptedException {

List<Integer> list = new ArrayList<>();

InsSingleton insSingleton = new InsSingleton();

for (int i = 0; i < 10000; i++) {

new Thread(insSingleton).start();
}

list = insSingleton.list;

Set<Integer> set = new HashSet<>();
Set<Integer> set1 = new HashSet<>();

for (int i = 0; i < list.size(); i++) {
// checking what hashcode are added in list
if (set.add(list.get(i)))
set1.add(list.get(i));

}

// printing the hashcodes
System.out.println(list);
System.out.println(set1);

}


public class SingleTon {

private static SingleTon singleTon = null;

//private constructor
private SingleTon() {

}

//synchronized method
public static synchronized SingleTon getInstance() {

if (singleTon == null) {
singleTon = new SingleTon();
}

return singleTon;

}

}







 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amithk kumar wrote:I have created a main class and ran a thread for 10000 tines each time it hits run method it will call l SingleTon.getInstance method and adds hashcode to a list but i am wondering why iam getting

output like this even though the getinstance method of SingleTon is synchronized


The ArrayList class isn't thread safe. You have 10,000 threads trying to use the same one simultaneously. You should wrap the array list with the Collections synchronizedList() method.

Henry
 
amithk kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even though using collections.synchronized list the output is having null and other hashcode value
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amithk kumar wrote:even though using collections.synchronized list the output is having null and other hashcode value


TellTheDetails. How are you using it? Show us your updated InsSingleton class.
 
amithk kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the class

public class InsSingleton implements Runnable {

public List<Integer> list = new ArrayList<>();

public int cnt = 0;


// Adding hashcode values to a list
@Override
public void run() {

list.add(SingleTon.getInstance().hashCode());

Collections.synchronizedList(list);

}

}



 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amithk kumar wrote:here is the class

public class InsSingleton implements Runnable {

public List<Integer> list = new ArrayList<>();

public int cnt = 0;


// Adding hashcode values to a list
@Override
public void run() {

list.add(SingleTon.getInstance().hashCode());

Collections.synchronizedList(list);

}

}


wrapping it means doing something like this:





 
amithk kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think my eclipse is behaving weird actually when getinstance is returning null the getinstance.hashcode should throw null pointer exception but sometimes aim able to see concurrentmodificationexception
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just try to wrap your list correctly e.g.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!