Android 學習日誌(5) 執行緒
阿新 • • 發佈:2018-12-19
package com.example.test; import android.app.Service; import android.content.Intent; import android.content.res.Configuration; import android.os.IBinder; import android.util.Log; import java.util.concurrent.locks.ReentrantLock; //Log.d -- 在兩條執行緒一起呼叫時基本沒有出現亂碼及其他問題 所以應該是執行緒安全的 public class MyTestService extends Service { public class MyThread extends Thread { final private String Tag = "MyThread"; private ReentrantLock m_ThreadLocker = new ReentrantLock(); private boolean m_IsStopping = true; private int taskCount = 10; MyThread(String threadName) { super(threadName); } @Override public void run() { setStopped(false); while (true) { if (isStoped()) break; //do task... try { Log.d(Tag, "Begin handle tasks..."); while (taskCount > 0) { Thread.currentThread().sleep(16); Log.d(Tag, "Running Tasks..." + taskCount--); } Log.d(Tag, "Ended handle tasks..."); if (taskCount == 0 && !isStoped()) taskCount = 10; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void terminate(boolean wait_for_last_task_finish) { if (isStoped()) Log.d(Tag, "Already Stopped!"); setStopped(true); if (wait_for_last_task_finish) { try { waitForTask(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Log.d(Tag, "Try Yield Thread"); yield(); Log.d(Tag, "Thread Yield..."); } public boolean isStoped() { boolean ret = false; m_ThreadLocker.lock(); ret = m_IsStopping; m_ThreadLocker.unlock(); return ret; } private void waitForTask() throws InterruptedException { taskCount = 500; this.join(); } private void setStopped(boolean stop) { m_ThreadLocker.lock(); m_IsStopping = stop; m_ThreadLocker.unlock(); } } static String Tag = "[MyTestService] : "; private final MyThread m_ServiceRunnable = new MyThread("MyTestServiceRunnable"); @Override public void onCreate() { OutLog("onCreate"); super.onCreate(); doTasks(); } //Methods// public void doTasks() { OutLog(Thread.currentThread().getName()); m_ServiceRunnable.start(); int i = 100; while (true) { try { Thread.currentThread().sleep(16); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (i-- > 0) OutLog("doTasks..." + i); if (i < 0 && !m_ServiceRunnable.isStoped()) m_ServiceRunnable.terminate(true); } } //可傳入空 @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub OutLog("OnBind"); return null; } /* * START_STICKY_COMPATIBILITY = 0 START_STICKY = 1 START_NOT_STICKY = 2 START_REDELIVER_INTENT = 3 * */ @Override public int onStartCommand(Intent intent, int flags, int startId) { int ret = super.onStartCommand(intent, flags, startId); OutLog("onStartCommand : flag = [" + flags + "] startId = [" + startId + "]" + " return = [" + ret + "]"); return ret; } @Override public void onStart(Intent intent, int startId) { OutLog("onStart"); super.onStart(intent, startId); } @Override public void onDestroy() { OutLog("onDestroy"); super.onDestroy(); } @Override public void onConfigurationChanged(Configuration newConfig) { OutLog("onConfigurationChanged"); super.onConfigurationChanged(newConfig); } @Override public void onLowMemory() { OutLog("onLowMemory"); super.onLowMemory(); } @Override public void onTrimMemory(int level) { OutLog("onTrimMemory"); super.onTrimMemory(level); } public boolean onUnbind(Intent intent) { OutLog("onUnbind"); return super.onUnbind(intent); } public void onRebind(Intent intent) { OutLog("onRebind"); super.onRebind(intent); } public void onTaskRemoved(Intent rootIntent) { OutLog("onTaskRemoved"); super.onTaskRemoved(rootIntent); } private void OutLog(String str) { Log.d(Tag, str); } }