[关闭]
@mSolo 2015-04-29T00:00:11.000000Z 字数 5753 阅读 1542

Android 自学之路(四)

Android 并发


Android 并发技术概览图

线程安全之基本工具

  1. synchronized (this) { // (1)
  2. // Execute code (2)
  3. wait(); // (3)
  4. // Execute code (4)
  5. } // (5)
  1. synchronized void changeState() { sharedResource++; } // (1)
  2. void changeState() { // (2)
  3. synchronized(this) {
  4. sharedResource++;
  5. }
  6. }
  7. private final Object mLock = new Object(); // (3)
  8. void changeState() {
  9. synchronized(mLock) {
  10. sharedResource++;
  11. }
  12. }
  13. synchronized static void changeState() { staticSharedResource++; } // (4)
  14. static void changeState() { // (5)
  15. synchronized(this.getClass()) {
  16. staticSharedResource++;
  17. }
  18. }
  1. int sharedResource;
  2. private ReentrantLock mLock = new ReentrantLock();
  3. public void changeState() {
  4. mLock.lock();
  5. try {
  6. sharedResource++;
  7. }
  8. finally {
  9. mLock.unlock();
  10. }
  11. }

Android 线程基础

线程间通信

  1. int BUFFER_SIZE_IN_CHARS = 1024 * 4;
  2. PipedReader r = new PipedReader(BUFFER_SIZE_IN_CHARS);
  3. PipedWriter w = new PipedWriter(r);
  1. private final int LIMIT = 10;
  2. private BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<Integer>(LIMIT);
  1. public class LooperActivity extends Activity {
  2. LooperThread mLooperThread;
  3. private static class LooperThread extends Thread {
  4. public Handler mHandler;
  5. public void run() {
  6. Looper.prepare();
  7. mHandler = new Handler() {
  8. public void handleMessage(Message msg) {
  9. if(msg.what == 0) {
  10. doLongRunningOperation();
  11. }
  12. }
  13. };
  14. Looper.loop();
  15. }
  16. }
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. mLooperThread = new LooperThread();
  20. mLooperThread.start();
  21. }
  22. public void onClick(View v) {
  23. if (mLooperThread.mHandler != null) {
  24. Message msg = mLooperThread.mHandler.obtainMessage(0);
  25. mLooperThread.mHandler.sendMessage(msg);
  26. }
  27. }
  28. private void doLongRunningOperation() {
  29. // Add long running operation here.
  30. }
  31. protected void onDestroy() {
  32. mLooperThread.mHandler.getLooper().quit();
  33. }
  34. }
  1. MessageQueue mq = Looper.myQueue();
  2. // Create and register/unregister an idle listener.
  3. MessageQueue.IdleHandler idleHandler = new MessageQueue.IdleHandler();
  4. mq.addIdleHandler(idleHandler)
  5. mq.removeIdleHandler(idleHandler)
  6. interface IdleHandler {
  7. boolean queueIdle();
  8. }
  1. Runnable task = new Runnable() {...};
  2. new Handler(Looper.getMainLooper()).post(task);
  3. private void postFromUiThreadToUiThread() {
  4. new Handler().post(new Runnable() { ... });
  5. // The code at this point is part of a message being processed
  6. // and is executed before the posted message.
  7. }
  8. private void postFromUiThreadToUiThread() {
  9. runOnUiThread(new Runnable() { ... });
  10. }
  1. public class EatApplication extends Application {
  2. private long mUiThreadId;
  3. private Handler mUiHandler;
  4. @Override
  5. public void onCreate() {
  6. super.onCreate();
  7. mUiThreadId = Thread.currentThread().getId();
  8. mUiHandler = new Handler();
  9. }
  10. public void customRunOnUiThread(Runnable action) {
  11. if (Thread.currentThread().getId() != mUiThreadId) {
  12. mUiHandler.post(action);
  13. } else {
  14. action.run();
  15. }
  16. }
  17. }

Uncaught Exception

  1. // Set new global handler
  2. Thread.setDefaultUncaughtExceptionHandler(new ErrorReportExceptionHandler());
  3. // Error handler that redirects exception to the system default handler.
  4. public class ErrorReportExceptionHandler
  5. implements Thread.UncaughtExceptionHandler {
  6. private final Thread.UncaughtExceptionHandler defaultHandler;
  7. public ErrorReportExceptionHandler() {
  8. this.defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
  9. }
  10. @Override
  11. public void uncaughtException(Thread thread, Throwable throwable) {
  12. reportErrorToFile(throwable);
  13. defaultHandler.uncaughtException(thread, throwable);
  14. }
  15. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注