#BlockingQueue in spin lock won't break out unless I use debugger then it works forever

3 messages · Page 1 of 1 (latest)

twin geode
#

I have a thread with this:

    private final LinkedBlockingQueue<SymbolState> symbolRefreshQueue = new LinkedBlockingQueue<>();

    @Override
    public void run() {
        while (true) {
            try {
                if (SwankServer.INSTANCE.isActive()) {
                    while (symbolRefreshQueue.isEmpty())
                        Thread.sleep(1000);
                    List<SymbolState> refreshStates = new ArrayList<>();
                    symbolRefreshQueue.drainTo(refreshStates);
                    System.err.println("Refreshing " + refreshStates.size() + " symbols");
                    try {
                        refreshSymbols(refreshStates);
                    } catch (Exception e) {
                        // ignored
                    }
                }
            } catch (InterruptedException exception) {

            }
        }
    }

When I run the application it does nothing at all. Once I put a breakpoint at List<SymbolState> refreshStates = new ArrayList<>(); line and then run it suddenly works just fine and I get the input:

Refreshing 27 symbols
Refreshing 1 symbols```

I am completely baffled why is that...
sleek summitBOT
#

This post has been reserved for your question.

Hey @twin geode! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.