I have 6 threads accessing one common queue container, they have this in their run() method:
if (queue.size() != 0) {
Element e = queue.remove();
//code
It works as intended almost every time, however, once in a while thread switches just between the if check and remove() method, so some other thread gets the element and this thread gets a NoSuchElementException. How do i make sure that between those two lines of code threads won't switch?