There is an ExecutorService that has 3 threads that each one add Pair<String,Integer> objects to a TreeSet<Pair<String,Integer>>. The addToSet() method is declared synchronized and everything works fine. But I must implement a new thread that execute a scheduled task that has to access this Set and print all the values. The problem is that sometimes the scheduled thread crashes(the other threads work fine). I think that it crashes because the Set is being modified by the other 3 threads during the for loop of the printer(scheduled thread).
There is an ExecutorService that has 3 threads that each one add Pair<String,Integer> objects
Share
You need mutual exclusion between the
addToSetmethod and the method which prints all values. One way to do that: