Permalink
Browse files

track threads, onDestroy complies with specification

Change-Id: I7cf6ae78aab9ed6224b6b72025819cf8f56d97f7
Reviewed-on: http://review.couchbase.org/9498
Reviewed-by: Marty Schoch <marty.schoch@gmail.com>
Reviewed-by: Chris Anderson <jchris@couchbase.com>
Tested-by: Dale Harvey <daleharvey@arandomurl.com>
Reviewed-by: Dale Harvey <daleharvey@arandomurl.com>
  • Loading branch information...
1 parent 4ab6970 commit 35ae580f5c57d56b4f4cc927e68a63c4dca5dd2a @mschoch mschoch committed with Dale Harvey Sep 9, 2011
Showing with 47 additions and 13 deletions.
  1. +47 −13 src/com/couchbase/android/CouchbaseService.java
@@ -95,6 +95,16 @@
private BufferedReader err;
/**
+ * Thread responsible for installing Couchbase
+ */
+ private CouchbaseInstaller couchbaseInstallThread;
+
+ /**
+ * Thread responsible for communicating with Couchbase process
+ */
+ private Thread couchbaseRunThread;
+
+ /**
* A handler to pass messages between Couchbase main thread, Couchbase installer thread, and application main thread
*
* Couchbase runs in a seperate thread, have the communication run
@@ -128,6 +138,7 @@ else if(msg.obj instanceof String){
}
break;
case COMPLETE:
+ couchbaseInstallThread = null;
startCouchbaseService();
break;
case COUCHBASE_STARTED:
@@ -145,9 +156,7 @@ else if(msg.obj instanceof String){
*/
@Override
public void onDestroy() {
- if (couchbaseStarted) {
- stop();
- }
+ stop();
couchbaseDelegate = null;
}
@@ -202,7 +211,8 @@ void couchbaseStarted() {
* @param pkg the package identifier to verify and install if necessary
*/
private void installCouchbase(final String pkg) {
- new CouchbaseInstaller(pkg, mHandler, this).start();
+ couchbaseInstallThread = new CouchbaseInstaller(pkg, mHandler, this);
+ couchbaseInstallThread.start();
}
/**
@@ -243,7 +253,7 @@ void startCouchbaseService() {
out = new PrintStream(process.getOutputStream());
err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
- new Thread(new Runnable() {
+ couchbaseRunThread = new Thread(new Runnable() {
@Override
public void run() {
try {
@@ -277,12 +287,12 @@ public void run() {
}
finally {
couchbaseStarted = false;
- couchbaseStopping = false;
stopSelf();
}
}
}
- }).start();
+ });
+ couchbaseRunThread.start();
} catch (IOException ioe) {
@@ -294,12 +304,36 @@ public void run() {
/**
* Stop the Couchbase process
*/
- private void stop() {
- if(couchbaseStarted && !couchbaseStopping) {
- couchbaseStopping = true;
- process.destroy();
- }
- }
+ private void stop() {
+ if(!couchbaseStopping) {
+ couchbaseStopping = true;
+
+ try {
+ //if installation is running, cancel it and wait for it to finish
+ if(couchbaseInstallThread != null) {
+ couchbaseInstallThread.cancelInstallation();
+ couchbaseInstallThread.join();
+ }
+
+ //if couchbase process is running kill the process
+ if(process != null) {
+ process.destroy();
+ process = null;
+ }
+
+ //now wait for the thread to finish
+ if(couchbaseRunThread != null) {
+ couchbaseRunThread.join();
+ couchbaseRunThread = null;
+ }
+ } catch (InterruptedException e) {
+ Log.v(CouchbaseMobile.TAG, "Interrupted while waiting for threads to die");
+ }
+ finally {
+ couchbaseStopping = false;
+ }
+ }
+}
/**
* Utility function to parse a string of text for URLs

0 comments on commit 35ae580

Please sign in to comment.