• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Singleton is behaving different with synchronized method

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;

}

}







 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even though using collections.synchronized list the output is having null and other hashcode value
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

}

}



 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just try to wrap your list correctly e.g.

 
reply
    Bookmark Topic Watch Topic
  • New Topic