Skip to content
Snippets Groups Projects
Commit 239e532f authored by akwizgran's avatar akwizgran
Browse files

A ScheduledExecutorService is overkill for one task; use a Thread.

parent 80ac368c
No related branches found
No related tags found
No related merge requests found
package net.sf.briar.plugins.bluetooth; package net.sf.briar.plugins.bluetooth;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static javax.bluetooth.DiscoveryAgent.GIAC; import static javax.bluetooth.DiscoveryAgent.GIAC;
...@@ -12,9 +11,6 @@ import java.util.Map; ...@@ -12,9 +11,6 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -55,12 +51,9 @@ class BluetoothPlugin implements DuplexPlugin { ...@@ -55,12 +51,9 @@ class BluetoothPlugin implements DuplexPlugin {
private final DuplexPluginCallback callback; private final DuplexPluginCallback callback;
private final long maxLatency, pollingInterval; private final long maxLatency, pollingInterval;
private final Semaphore discoverySemaphore = new Semaphore(1); private final Semaphore discoverySemaphore = new Semaphore(1);
private final ScheduledExecutorService scheduler;
private volatile boolean running = false; private volatile boolean running = false;
private volatile StreamConnectionNotifier socket = null; private volatile StreamConnectionNotifier socket = null;
// Non-null if running has ever been true
private volatile LocalDevice localDevice = null; private volatile LocalDevice localDevice = null;
BluetoothPlugin(Executor pluginExecutor, Clock clock, BluetoothPlugin(Executor pluginExecutor, Clock clock,
...@@ -72,7 +65,6 @@ class BluetoothPlugin implements DuplexPlugin { ...@@ -72,7 +65,6 @@ class BluetoothPlugin implements DuplexPlugin {
this.callback = callback; this.callback = callback;
this.maxLatency = maxLatency; this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval; this.pollingInterval = pollingInterval;
scheduler = Executors.newScheduledThreadPool(0);
} }
public TransportId getId() { public TransportId getId() {
...@@ -176,7 +168,6 @@ class BluetoothPlugin implements DuplexPlugin { ...@@ -176,7 +168,6 @@ class BluetoothPlugin implements DuplexPlugin {
public void stop() { public void stop() {
running = false; running = false;
if(socket != null) tryToClose(socket); if(socket != null) tryToClose(socket);
scheduler.shutdownNow();
} }
public boolean shouldPoll() { public boolean shouldPoll() {
...@@ -276,7 +267,7 @@ class BluetoothPlugin implements DuplexPlugin { ...@@ -276,7 +267,7 @@ class BluetoothPlugin implements DuplexPlugin {
} }
public DuplexTransportConnection acceptInvitation(PseudoRandom r, public DuplexTransportConnection acceptInvitation(PseudoRandom r,
long timeout) { final long timeout) {
if(!running) return null; if(!running) return null;
// Use the same pseudo-random UUID as the contact // Use the same pseudo-random UUID as the contact
byte[] b = r.nextBytes(UUID_BYTES); byte[] b = r.nextBytes(UUID_BYTES);
...@@ -297,13 +288,19 @@ class BluetoothPlugin implements DuplexPlugin { ...@@ -297,13 +288,19 @@ class BluetoothPlugin implements DuplexPlugin {
return null; return null;
} }
// Close the socket when the invitation times out // Close the socket when the invitation times out
Runnable close = new Runnable() { new Thread() {
@Override
public void run() { public void run() {
try {
Thread.sleep(timeout);
} catch(InterruptedException e) {
if(LOG.isLoggable(WARNING))
LOG.warning("Interrupted while waiting for invitation");
}
tryToClose(scn); tryToClose(scn);
} }
}; }.start();
ScheduledFuture<?> f = scheduler.schedule(close, timeout, MILLISECONDS); // Try to accept a connection
// Try to accept a connection and close the socket
try { try {
StreamConnection s = scn.acceptAndOpen(); StreamConnection s = scn.acceptAndOpen();
return new BluetoothTransportConnection(s, maxLatency); return new BluetoothTransportConnection(s, maxLatency);
...@@ -311,8 +308,6 @@ class BluetoothPlugin implements DuplexPlugin { ...@@ -311,8 +308,6 @@ class BluetoothPlugin implements DuplexPlugin {
// This is expected when the socket is closed // This is expected when the socket is closed
if(LOG.isLoggable(INFO)) LOG.log(INFO, e.toString(), e); if(LOG.isLoggable(INFO)) LOG.log(INFO, e.toString(), e);
return null; return null;
} finally {
if(f.cancel(false)) tryToClose(scn);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment