1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ddmuilib; 18 19 import com.android.ddmlib.Log; 20 21 /** 22 * base background thread class. The class provides a synchronous quit method 23 * which sets a quitting flag to true. Inheriting classes should regularly test 24 * this flag with <code>isQuitting()</code> and should finish if the flag is 25 * true. 26 */ 27 public abstract class BackgroundThread extends Thread { 28 private boolean mQuit = false; 29 30 /** 31 * Tell the thread to exit. This is usually called from the UI thread. The 32 * call is synchronous and will only return once the thread has terminated 33 * itself. 34 */ quit()35 public final void quit() { 36 mQuit = true; 37 Log.d("ddms", "Waiting for BackgroundThread to quit"); 38 try { 39 this.join(); 40 } catch (InterruptedException ie) { 41 ie.printStackTrace(); 42 } 43 } 44 45 /** returns if the thread was asked to quit. */ isQuitting()46 protected final boolean isQuitting() { 47 return mQuit; 48 } 49 50 } 51