Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56e5e33

Browse files
committed
Make SocketThread daemon flag configurable via constructors
1 parent 72712e5 commit 56e5e33

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/main/java/de/taimos/gpsd4java/backend/GPSdEndpoint.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class GPSdEndpoint {
6565

6666
private SocketThread listenThread;
6767

68+
private final boolean daemon;
69+
6870
private final List<IObjectListener> listeners = new ArrayList<IObjectListener>(1);
6971

7072
private IGPSObject asyncResult = null;
@@ -88,11 +90,13 @@ public class GPSdEndpoint {
8890
*
8991
* @param server the server name or IP
9092
* @param port the server port
91-
* @param resultParser
93+
* @param resultParser the result parser
94+
* @param daemon whether to start the underlying socket thread as a daemon, as defined in {@link Thread#setDaemon}
9295
* @throws UnknownHostException
9396
* @throws IOException
9497
*/
95-
public GPSdEndpoint(final String server, final int port, final AbstractResultParser resultParser) throws UnknownHostException, IOException {
98+
public GPSdEndpoint(final String server, final int port, final AbstractResultParser resultParser, final boolean daemon)
99+
throws UnknownHostException, IOException {
96100
this.server = server;
97101
this.port = port;
98102
if (server == null) {
@@ -109,13 +113,28 @@ public GPSdEndpoint(final String server, final int port, final AbstractResultPar
109113
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
110114
this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
111115
this.resultParser = resultParser;
116+
117+
this.daemon = daemon;
118+
}
119+
120+
/**
121+
* Instantiate this class to connect to a GPSd server
122+
*
123+
* @param server the server name or IP
124+
* @param port the server port
125+
* @param resultParser the result parser
126+
* @throws UnknownHostException
127+
* @throws IOException
128+
*/
129+
public GPSdEndpoint(final String server, final int port, final AbstractResultParser resultParser) throws UnknownHostException, IOException {
130+
this(server, port, resultParser, true);
112131
}
113132

114133
/**
115134
* start the endpoint
116135
*/
117136
public void start() {
118-
this.listenThread = new SocketThread(this.in, this, this.resultParser);
137+
this.listenThread = new SocketThread(this.in, this, this.resultParser, this.daemon);
119138
this.listenThread.start();
120139
}
121140

@@ -324,7 +343,7 @@ void handleDisconnected() throws IOException {
324343
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
325344
this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
326345

327-
this.listenThread = new SocketThread(this.in, this, this.resultParser);
346+
this.listenThread = new SocketThread(this.in, this, this.resultParser, this.daemon);
328347
this.listenThread.start();
329348
if (lastWatch != null) { // restore watch if we had one.
330349
this.syncCommand(lastWatch, WatchObject.class);

src/main/java/de/taimos/gpsd4java/backend/SocketThread.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ public class SocketThread extends Thread {
4747
/**
4848
* @param reader the socket input
4949
* @param endpoint the endpoint
50-
* @param resultParser
50+
* @param resultParser the result parser
51+
* @param daemon whether to configure the thread as a daemon, as defined in {@link Thread#setDaemon}
5152
*/
52-
public SocketThread(final BufferedReader reader, final GPSdEndpoint endpoint, final AbstractResultParser resultParser) {
53+
public SocketThread(final BufferedReader reader, final GPSdEndpoint endpoint,
54+
final AbstractResultParser resultParser, final boolean daemon) {
5355
if (reader == null) {
5456
throw new IllegalArgumentException("reader can not be null!");
5557
}
@@ -64,9 +66,19 @@ public SocketThread(final BufferedReader reader, final GPSdEndpoint endpoint, fi
6466
this.endpoint = endpoint;
6567
this.resultParser = resultParser;
6668

69+
this.setDaemon(daemon);
6770
this.setName("GPS Socket Thread");
6871
}
6972

73+
/**
74+
* @param reader the socket input
75+
* @param endpoint the endpoint
76+
* @param resultParser the result parser
77+
*/
78+
public SocketThread(final BufferedReader reader, final GPSdEndpoint endpoint, final AbstractResultParser resultParser) {
79+
this(reader, endpoint, resultParser, true);
80+
}
81+
7082
@Override
7183
public void run() {
7284
while (this.running.get()) {

0 commit comments

Comments
 (0)