use AtomicReference.

This commit is contained in:
Eric House 2018-01-01 19:48:01 -08:00
parent b24a004cab
commit e5ae940216

View file

@ -62,6 +62,7 @@ import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
public class RelayService extends XWService public class RelayService extends XWService
implements NetStateCache.StateChangedIf { implements NetStateCache.StateChangedIf {
@ -113,7 +114,7 @@ public class RelayService extends XWService
static { resetBackoffTimer(); } static { resetBackoffTimer(); }
private Thread m_fetchThread = null; // no longer used private Thread m_fetchThread = null; // no longer used
private UDPThreads m_UDPThreads = null; private AtomicReference<UDPThreads> m_UDPThreadsRef = new AtomicReference<>();
private Handler m_handler; private Handler m_handler;
private Runnable m_onInactivity; private Runnable m_onInactivity;
private int m_maxIntervalSeconds = 0; private int m_maxIntervalSeconds = 0;
@ -554,11 +555,12 @@ public class RelayService extends XWService
private void startUDPThreadsIfNot() private void startUDPThreadsIfNot()
{ {
if ( XWApp.UDP_ENABLED && relayEnabled( this ) ) { if ( XWApp.UDP_ENABLED && relayEnabled( this ) ) {
if ( null == m_UDPThreads ) { synchronized ( m_UDPThreadsRef ) {
m_UDPThreads = new UDPThreads(); if ( null == m_UDPThreadsRef.get() ) {
m_UDPThreads.start(); UDPThreads threads = new UDPThreads();
} else { m_UDPThreadsRef.set( threads );
// Log.i( TAG, "m_UDPReadThread not null and assumed to be running" ); threads.start();
}
} }
} else { } else {
Log.i( TAG, "startUDPThreadsIfNot(): UDP disabled" ); Log.i( TAG, "startUDPThreadsIfNot(): UDP disabled" );
@ -608,9 +610,9 @@ public class RelayService extends XWService
{ {
DbgUtils.assertOnUIThread(); DbgUtils.assertOnUIThread();
if ( null != m_UDPThreads ) { UDPThreads threads = m_UDPThreadsRef.getAndSet( null );
m_UDPThreads.stop(); if ( null != threads ) {
m_UDPThreads = null; threads.stop();
} }
} }
@ -935,7 +937,10 @@ public class RelayService extends XWService
private void postPacket( ByteArrayOutputStream bas, XWRelayReg cmd ) private void postPacket( ByteArrayOutputStream bas, XWRelayReg cmd )
{ {
startUDPThreadsIfNot(); startUDPThreadsIfNot();
m_UDPThreads.add( new PacketData( bas, cmd ) ); UDPThreads threads = m_UDPThreadsRef.get();
if ( threads != null ) {
threads.add( new PacketData( bas, cmd ) );
}
// 0 ok; thread will often have sent already! // 0 ok; thread will often have sent already!
// DbgUtils.logf( "postPacket() done; %d in queue", m_queue.size() ); // DbgUtils.logf( "postPacket() done; %d in queue", m_queue.size() );
} }