Plugins should use the executor rather than creating threads.

This commit is contained in:
akwizgran
2011-12-08 16:57:24 +00:00
parent c6b6c20205
commit 9f0b865ba8
7 changed files with 59 additions and 90 deletions

View File

@@ -126,7 +126,11 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
}
socket = scn;
}
startContactAccepterThread();
pluginExecutor.execute(new Runnable() {
public void run() {
acceptContactConnections();
}
});
}
private synchronized String getUuid() {
@@ -160,15 +164,6 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
}
}
private void startContactAccepterThread() {
new Thread() {
@Override
public void run() {
acceptContactConnections();
}
}.start();
}
private void acceptContactConnections() {
while(true) {
StreamConnectionNotifier scn;
@@ -299,9 +294,17 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
// The invitee's device may not be discoverable, so both parties must
// try to initiate connections
String uuid = convertInvitationCodeToUuid(code);
ConnectionCallback c = new ConnectionCallback(uuid, timeout);
startOutgoingInvitationThread(c);
startIncomingInvitationThread(c);
final ConnectionCallback c = new ConnectionCallback(uuid, timeout);
pluginExecutor.execute(new Runnable() {
public void run() {
createInvitationConnection(c);
}
});
pluginExecutor.execute(new Runnable() {
public void run() {
bindInvitationSocket(c);
}
});
try {
StreamConnection s = c.waitForConnection();
return s == null ? null : new BluetoothTransportConnection(s);
@@ -319,15 +322,6 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
return StringUtils.toHexString(b);
}
private void startOutgoingInvitationThread(final ConnectionCallback c) {
new Thread() {
@Override
public void run() {
createInvitationConnection(c);
}
}.start();
}
private void createInvitationConnection(ConnectionCallback c) {
DiscoveryAgent discoveryAgent;
synchronized(this) {
@@ -369,30 +363,25 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
}
}
private void startIncomingInvitationThread(final ConnectionCallback c) {
new Thread() {
@Override
public void run() {
bindInvitationSocket(c);
}
}.start();
}
private void bindInvitationSocket(ConnectionCallback c) {
private void bindInvitationSocket(final ConnectionCallback c) {
synchronized(this) {
if(!started) return;
makeDeviceDiscoverable();
}
// Bind the socket
String url = "btspp://localhost:" + c.getUuid() + ";name=RFCOMM";
StreamConnectionNotifier scn;
final StreamConnectionNotifier scn;
try {
scn = (StreamConnectionNotifier) Connector.open(url);
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
return;
}
startInvitationAccepterThread(c, scn);
pluginExecutor.execute(new Runnable() {
public void run() {
acceptInvitationConnection(c, scn);
}
});
// Close the socket when the invitation times out
try {
Thread.sleep(c.getTimeout());
@@ -408,16 +397,6 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
}
}
private void startInvitationAccepterThread(final ConnectionCallback c,
final StreamConnectionNotifier scn) {
new Thread() {
@Override
public void run() {
acceptInvitationConnection(c, scn);
}
}.start();
}
private void acceptInvitationConnection(ConnectionCallback c,
StreamConnectionNotifier scn) {
synchronized(this) {