I've a singleton class like this:
private static StringsHandler INSTANCE = null;
private int count = 0;
//I have 2 methods so I don't have to be sending/checking nulls on getInstance
//once it's created
public static void createInstance(List<String> strings){
if(StringsHandler.INSTANCE == null)
StringsHandler.INSTANCE = new StringsHandler(strings);
}
public static StringsHandler getInstance(){
return StringsHandler.INSTANCE;
}
public synchronized File use(){
count++;
System.out.println("threads using .use " + count);
while(true){} //Keep the thread here so count should stay as 1
}
Instance is created on the application main class, main method just like this:
if(stringList.isEmpty()){
LOGGER.error("List was empty");
System.exit(0);
}
StringsHandler.createInstance(stringList);
And I call it using something like this:
list.parallelStream().forEach(element -> {
SingletonClass.getInstance().use();
});
This should print threads using .use 1
but it prints threads using .use 5
why is the synchronized keyword allowing for more than 1 thread?