Archiviert
13
0

Use an atomic integer instead of a volatile field.

Incrementing and decrementing is not thread-safe on volatile variables.
Dieser Commit ist enthalten in:
Kristian S. Stangeland 2012-10-03 23:13:05 +02:00
Ursprung c6fb01e1e1
Commit 558eab2253

Datei anzeigen

@ -1,6 +1,7 @@
package com.comphenix.protocol.async; package com.comphenix.protocol.async;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -28,7 +29,7 @@ public class AsyncListenerHandler {
private volatile boolean cancelled; private volatile boolean cancelled;
// If we've started the listener loop before // If we've started the listener loop before
private volatile int started; private AtomicInteger started = new AtomicInteger();
// The packet listener // The packet listener
private PacketListener listener; private PacketListener listener;
@ -153,7 +154,7 @@ public class AsyncListenerHandler {
throw new IllegalStateException("Listener has been cancelled. Create a new listener instead."); throw new IllegalStateException("Listener has been cancelled. Create a new listener instead.");
// Proceed // Proceed
started++; started.incrementAndGet();
try { try {
mainLoop: mainLoop:
@ -198,7 +199,7 @@ public class AsyncListenerHandler {
// We're done // We're done
} finally { } finally {
// Clean up // Clean up
started--; started.decrementAndGet();
close(); close();
} }
} }
@ -221,7 +222,7 @@ public class AsyncListenerHandler {
// Poison Pill Shutdown // Poison Pill Shutdown
queuedPackets.clear(); queuedPackets.clear();
for (int i = 0; i < started; i++) for (int i = 0; i < started.get(); i++)
queuedPackets.add(INTERUPT_PACKET); queuedPackets.add(INTERUPT_PACKET);
} }
} }