Missing update in ConcurrentLinkedQueue in Java

32 Views Asked by At

I have the following code in a java springboot app:

class myClass1 {
    void addToQueue() {
        logger.info("adding to queue");
        myClass2.add(trade);
    }
}
class myClass2 {
    ConcurrentLinkedQueue<Trade> executionQueue = new ConcurrentLinkedQueue<>();
    Trade tradeInExecution;

    void add(Trade: trade) {
        executionQueue.add(trade);
    }

    void execute() {
        var tradeInExecution = executionQueue.poll();
        if (tradeInExecution == null) {
            return;
        }
        logger.info("executing");
        // do some processing
    }
}
import org.springframework.scheduling.TaskScheduler;
@Component
public class CustomTaskScheduler {
    private final TaskScheduler taskScheduler;

    @PostConstruct
    public void scheduleTasks() {
        taskScheduler.scheduleWithFixedDelay(myClass2::execute, Instant.now(), Duration.ofMillis(100));
    }
}

I have also to add that myClass1.addToQueue() is getting exectued concurrently.

The problem is sometimes in the log I am having "adding to queue" and no "executing" after it. How could that be happening?

I am expecting for every "adding to queue" to have "executing" at some point, but some are missing "executing"

0

There are 0 best solutions below