@mSolo
2015-04-29T08:00:11.000000Z
字数 5753
阅读 1584
Android
并发
synchronized (this) { // (1)
// Execute code (2)
wait(); // (3)
// Execute code (4)
} // (5)
synchronized void changeState() { sharedResource++; } // (1)
void changeState() { // (2)
synchronized(this) {
sharedResource++;
}
}
private final Object mLock = new Object(); // (3)
void changeState() {
synchronized(mLock) {
sharedResource++;
}
}
synchronized static void changeState() { staticSharedResource++; } // (4)
static void changeState() { // (5)
synchronized(this.getClass()) {
staticSharedResource++;
}
}
int sharedResource;
private ReentrantLock mLock = new ReentrantLock();
public void changeState() {
mLock.lock();
try {
sharedResource++;
}
finally {
mLock.unlock();
}
}
线程分类
线程调度(使用的是 Linux 的 CFS 而非 Android Frameworks)
setPriority(int priority)
Process.setThreadPriority(int priority); // calling thread
Process.setThreadPriority(int threadId, int priority);
int BUFFER_SIZE_IN_CHARS = 1024 * 4;
PipedReader r = new PipedReader(BUFFER_SIZE_IN_CHARS);
PipedWriter w = new PipedWriter(r);
private final int LIMIT = 10;
private BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<Integer>(LIMIT);
public class LooperActivity extends Activity {
LooperThread mLooperThread;
private static class LooperThread extends Thread {
public Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
if(msg.what == 0) {
doLongRunningOperation();
}
}
};
Looper.loop();
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLooperThread = new LooperThread();
mLooperThread.start();
}
public void onClick(View v) {
if (mLooperThread.mHandler != null) {
Message msg = mLooperThread.mHandler.obtainMessage(0);
mLooperThread.mHandler.sendMessage(msg);
}
}
private void doLongRunningOperation() {
// Add long running operation here.
}
protected void onDestroy() {
mLooperThread.mHandler.getLooper().quit();
}
}
MessageQueue mq = Looper.myQueue();
// Create and register/unregister an idle listener.
MessageQueue.IdleHandler idleHandler = new MessageQueue.IdleHandler();
mq.addIdleHandler(idleHandler)
mq.removeIdleHandler(idleHandler)
interface IdleHandler {
boolean queueIdle();
}
③ Android Message Passing - Message
Message m = new Message();
Message m = Message.obtain();
Message m = Message.obtain(Handler h);
Message m = Message.obtain(Handler h, int what);
Message m = Message.obtain(Handler h, int what, Object o);
Message m = Message.obtain(Handler h, int what, int arg1, int arg2);
Message m = Message.obtain(Handler h, int what, int arg1, int arg2, Object o);
Message m = Message.obtain(Handler h, Runnable task);
Message m = Message.obtain(Message originalMsg);
④ Android Message Passing - Looper
quit()
or quitSafely() // API level 18 (Jelly Bean 4.3)
⑤ Android Message Passing - Handler
new Handler();
、 new Handler(Handler.Callback)
new Handler(Looper);
、 new Handler(Looper, Handler.Callback);
Message obtainMessage(int what, int arg1, int arg2)
Message obtainMessage()
Message obtainMessage(int what, int arg1, int arg2, Object obj)
Message obtainMessage(int what)
Message obtainMessage(int what, Object obj)
boolean post(Runnable r)
boolean postAtFrontOfQueue(Runnable r)
boolean postAtTime(Runnable r, Object token, long uptimeMillis)
boolean postAtTime(Runnable r, long uptimeMillis)
boolean postDelayed(Runnable r, long delayMillis)
boolean sendMessage(Message msg)
boolean sendMessageAtFrontOfQueue(Message msg)
boolean sendMessageAtTime(Message msg, long uptimeMillis)
boolean sendMessageDelayed(Message msg, long delayMillis)
boolean sendEmptyMessage(int what)
boolean sendEmptyMessageAtTime(int what, long uptimeMillis)
boolean sendEmptyMessageDelayed(int what, long delayMillis)
removeCallbacks(Runnable r)
removeCallbacks(Runnable r, Object token)
removeMessages(int what)
removeMessages(int what, Object object)
removeCallbacksAndMessages(Object token)
mWorkerHandler.dump(new LogPrinter(Log.DEBUG, TAG), "");
Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG, TAG));
⑥ Communicating with the UI Thread
Runnable task = new Runnable() {...};
new Handler(Looper.getMainLooper()).post(task);
private void postFromUiThreadToUiThread() {
new Handler().post(new Runnable() { ... });
// The code at this point is part of a message being processed
// and is executed before the posted message.
}
private void postFromUiThreadToUiThread() {
runOnUiThread(new Runnable() { ... });
}
public class EatApplication extends Application {
private long mUiThreadId;
private Handler mUiHandler;
@Override
public void onCreate() {
super.onCreate();
mUiThreadId = Thread.currentThread().getId();
mUiHandler = new Handler();
}
public void customRunOnUiThread(Runnable action) {
if (Thread.currentThread().getId() != mUiThreadId) {
mUiHandler.post(action);
} else {
action.run();
}
}
}
static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler);
void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler);
// Set new global handler
Thread.setDefaultUncaughtExceptionHandler(new ErrorReportExceptionHandler());
// Error handler that redirects exception to the system default handler.
public class ErrorReportExceptionHandler
implements Thread.UncaughtExceptionHandler {
private final Thread.UncaughtExceptionHandler defaultHandler;
public ErrorReportExceptionHandler() {
this.defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
reportErrorToFile(throwable);
defaultHandler.uncaughtException(thread, throwable);
}
}