Android中的多线程及线程间通信
目录
开启多线程的方式
Thread
Thread thread = new Thread() {
@Override
public void run() {
System.out.println("Thread started!");
}
};
thread.start();
Runnable
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("Thread with Runnable
started!");
}
};
Thread thread = new Thread(runnable);
thread.start();
ThreadFactory
ThreadFactory factory = new ThreadFactory() {
int count = 0;
@Override
public Thread newThread(Runnable r) {
count ++;
return new Thread(r, "Thread-" + count);
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()
+ " started!");
}
};
Thread thread = factory.newThread(runnable);
thread.start();
Thread thread1 = factory.newThread(runnable);
thread1.start();
Executor 和线程池
常⽤: newCachedThreadPool()
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("Thread with Runnable
started!");
}
};
Executor executor =
Executors.newCachedThreadPool();
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);
短时批量处理: newFixedThreadPool()
ExecutorService executor =
Executors.newFixedThreadPool(20);
for (Bitmap bitmap : bitmaps) {
executor.execute(bitmapProcessor(bitmap));
}
executor.shutdown();
Callable 和 Future
Callable<String> callable = new Callable<String>() {
@Override
public String call() {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Done!";
}
};
ExecutorService executor =
Executors.newCachedThreadPool();
Future<String> future = executor.submit(callable);
try {
String result = future.get();
System.out.println("result: " + result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
线程同步与线程安全
为什么要线程同步?为什么要保证线程安全?
在多个线程访问共同的资源时,在某⼀个线程对资源进⾏写操作的中途(写⼊已经开始,但还没结束),其他线程对这个写了⼀半的资源进⾏了读操作,或者基于这个写了⼀半的资源进⾏了写操作,导致出现数据错误
怎么保证线程安全?
synchronized
本质:保证⽅法内部或代码块内部资源(数据)的互斥访问。即同⼀时间、由同⼀个 Monitor 监视的代码,最多只能有⼀个线程在访问
volatile
1.保证加了 volatile 关键字的字段的操作具有同步性,以及对 long 和double 的操作的原⼦性
2.volatile 只对基本类型 (byte、char、short、int、long、flfloat、double、boolean) 的赋值操作和对象的引⽤赋值操作有效,你要修改 User.name 是不能保证同步的。
3.volatile 依然解决不了 ++ 的原⼦性问题。
ReentrantLock
ReentrantReadWriteLock
对于存在多线程读取得数据 存在下列三种情况
1.多个线程同时读
2.多个线程同时修改
3.多个线程同时修改或读
如果有数据仅仅是多个线程进行读的操作,则不需要加锁(此时不存在线程安全问题)为了效率可以使用ReentrantReadWriteLock
对会进行值改变的代码加写锁对进行值得访问得代码加读锁
ReentrantReadWriteLock lock = new
ReentrantReadWriteLock();
Lock readLock = lock.readLock();
Lock writeLock = lock.writeLock();
private int x = 0;
private void count() {
writeLock.lock();
try {
x++;
} finally {
writeLock.unlock();
}
}
private void print(int time) {
readLock.lock();
try {
for (int i = 0; i < time; i++) {
System.out.print(x + " ");
}
System.out.println();
} finally {
readLock.unlock();
}
}
锁机制的本质:
通过对共享资源进⾏访问限制,让同⼀时间只有⼀个线程可以访问资源,保证了数据的准确性。
线程间通信
⼀个线程启动别的线程
new Thread().start()、Executor.execute()
⼀个线程终结另⼀个线程
1.Thread.stop()
2.Thread.interrupt():温和式终结:不⽴即、不强制
interrupted() 和 isInterrupted():检查(和重置)中断状态
InterruptedException:如果在线程「等待」时中断,或者在中断状态「等待」,直接结束等待过程(因为等待过程什么也不会做,⽽interrupt() 的⽬的是让线程做完收尾⼯作后尽快终结,所以要跳过等待过程)
⼀个线程唤醒另⼀个线程
Object.wait() 和 Object.notify() / notifyAll()
Android中的多线程机制
Handler
Handler中所有发送消息的方法最终都会到enqueueMessage这个方法中
举个例子
public final boolean sendMessage(@NonNull Message msg) {
return sendMessageDelayed(msg, 0);
}
public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis) {
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
public boolean sendMessageAtTime(@NonNull Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
if (queue == null) {
RuntimeException e = new RuntimeException(
this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
return false;
}
return enqueueMessage(queue, msg, uptimeMillis);
}
private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg,
long uptimeMillis) {
msg.target = this;
msg.workSourceUid = ThreadLocalWorkSource.getUid();
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
可以看到调用sendMessage其实就是调用sendMessageDelayed并标记delay的时间为0,而调用sendMessageDelayed其实就是调用sendMessageAtTime并标记时间为delay的时间加上系统当前的时间,而sendMessageAtTime最后又调用了enqueueMessage最后将消息放入了消息队列中queue.enqueueMessage(msg, uptimeMillis).
Handler处理消息的整体流程其实就是Handler将消息发送到消息队列中,然后Looper从消息队列中取消息然后再发送给Handler处理。我们接下来看看具体是怎么实现的
先来看看消息队列MessageQueue的enqueueMessage方法
boolean enqueueMessage(Message msg, long when) {
if (msg.target == null) {
throw new IllegalArgumentException("Message must have a target.");
}
synchronized (this) {
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}
if (mQuitting) {
IllegalStateException e = new IllegalStateException(
msg.target + " sending message to a Handler on a dead thread");
Log.w(TAG, e.getMessage(), e);
msg.recycle();
return false;
}
msg.markInUse();
msg.when = when;
Message p = mMessages;
boolean needWake;
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
msg.next = p;
mMessages = msg;
needWake = mBlocked;
} else {
// Inserted within the middle of the queue. Usually we don't have to wake
// up the event queue unless there is a barrier at the head of the queue
// and the message is the earliest asynchronous message in the queue.
needWake = mBlocked && p.target == null && msg.isAsynchronous();
Message prev;
for (;;) {
prev = p;
p = p.next;
if (p == null || when < p.when) {
break;
}
if (needWake && p.isAsynchronous()) {
needWake = false;
}
}
msg.next = p; // invariant: p == prev.next
prev.next = msg;
}
// We can assume mPtr != 0 because mQuitting is false.
if (needWake) {
nativeWake(mPtr);
}
}
return true;
}
这其实是一个队列的插入操作,通过分析易知采用的头插法
mMessages就是队列的首个元素,如果满足p == null || when == 0 || when < p.when (即队列为空或当前消息需要立即发送或者当前消息的时间比第一个元素的时间还要小)那么就将当前消息插入到队列的头部
否则进入else代码块
在这里面就是根据消息的时间按时间前后顺序插入消息
消息已经放入队列,looper就会从队列中取数据,每一个线程都可以有着一个looper,但不是默认就有的,而是需要我们在线程中调用Looper.prepare()函数为这个线程创建一个looper,一个线程只能有一个looper但是可以有多个Handler
主线程中我们没有调用Looper.prepare()函数为什么也能使用Handler呢?这是因为Android已经帮我们做好了
在ActivityThread中的main函数中
public static void main(String[] args) {
...
Looper.prepareMainLooper();
...
}
在线程中调用完Looper.prepare()完后再调用Looper.loop()这个线程的Looper就会开始工作,把发送到这个线程的消息队列上的数据取到并发送到对应的Handler处理
来看看Looper.prepare()做了些什么
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
将Looper放到了一个ThreadLocal中,ThreadLocal是一个方便用来存储以线程为作用域的数据,Looper便是这样的,来看看它是怎么做到的
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
首先取到调用这个函数的当前线程,由于是初次调用肯定会进入createMap(t, value);
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
可以看到给当前线程的threadLocals赋值了ThreadLocalMap对象
来看看ThreadLocal对应的构造函数
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}
然后通过Looper的
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
就可以拿到线程所对应的Looper
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
调用完Looper.prepare()完后再调用Looper.loop()这个线程的Looper就会开始工作,把发送到这个线程的消息队列上的数据取到并发送到对应的Handler处理,所以接下来看看
Looper.loop()函数
/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
if (me.mInLoop) {
Slog.w(TAG, "Loop again would have the queued messages be executed"
+ " before this one completed.");
}
me.mInLoop = true;
final MessageQueue queue = me.mQueue;
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
// Allow overriding a threshold with a system prop. e.g.
// adb shell 'setprop log.looper.1000.main.slow 1 && stop && start'
final int thresholdOverride =
SystemProperties.getInt("log.looper."
+ Process.myUid() + "."
+ Thread.currentThread().getName()
+ ".slow", 0);
boolean slowDeliveryDetected = false;
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// This must be in a local variable, in case a UI event sets the logger
final Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
// Make sure the observer won't change while processing a transaction.
final Observer observer = sObserver;
final long traceTag = me.mTraceTag;
long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs;
if (thresholdOverride > 0) {
slowDispatchThresholdMs = thresholdOverride;
slowDeliveryThresholdMs = thresholdOverride;
}
final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0);
final boolean logSlowDispatch = (slowDispatchThresholdMs > 0);
final boolean needStartTime = logSlowDelivery || logSlowDispatch;
final boolean needEndTime = logSlowDispatch;
if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
}
final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0;
final long dispatchEnd;
Object token = null;
if (observer != null) {
token = observer.messageDispatchStarting();
}
long origWorkSource = ThreadLocalWorkSource.setUid(msg.workSourceUid);
try {
msg.target.dispatchMessage(msg);
if (observer != null) {
observer.messageDispatched(token, msg);
}
dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
} catch (Exception exception) {
if (observer != null) {
observer.dispatchingThrewException(token, msg, exception);
}
throw exception;
} finally {
ThreadLocalWorkSource.restore(origWorkSource);
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
if (logSlowDelivery) {
if (slowDeliveryDetected) {
if ((dispatchStart - msg.when) <= 10) {
Slog.w(TAG, "Drained");
slowDeliveryDetected = false;
}
} else {
if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery",
msg)) {
// Once we write a slow delivery log, suppress until the queue drains.
slowDeliveryDetected = true;
}
}
}
if (logSlowDispatch) {
showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", msg);
}
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
}
msg.recycleUnchecked();
}
}
Message msg = queue.next(); // might block
//有人可能会对这行代码产生疑惑,如果消息队列没有数据了loop函数就退出了?
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
其实不会发生这种情况,如果消息队列的quit函数没有被调用的话,looper的loop方法是不会执行这行代码的,也就是说一定会拿到数据,这是因为当消息队列没有消息时便会休眠,此时就会阻塞在Message msg = queue.next()这行代码,直到消息队列中有数据后才会停止阻塞
消息队列的next()函数就是从消息队列中取数据的,我们来看看具体实现
Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
final long ptr = mPtr;
if (ptr == 0) {
return null;
}
int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
nativePollOnce(ptr, nextPollTimeoutMillis);
synchronized (this) {
// Try to retrieve the next message. Return if found.
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = mMessages;
if (msg != null && msg.target == null) {
// Stalled by a barrier. Find the next asynchronous message in the queue.
do {
prevMsg = msg;
msg = msg.next;
} while (msg != null && !msg.isAsynchronous());
}
if (msg != null) {
if (now < msg.when) {
// Next message is not ready. Set a timeout to wake up when it is ready.
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
// Got a message.
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
if (DEBUG) Log.v(TAG, "Returning message: " + msg);
msg.markInUse();
return msg;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
// Process the quit message now that all pending messages have been handled.
if (mQuitting) {
dispose();
return null;
}
// If first time idle, then get the number of idlers to run.
// Idle handles only run if the queue is empty or if the first message
// in the queue (possibly a barrier) is due to be handled in the future.
if (pendingIdleHandlerCount < 0
&& (mMessages == null || now < mMessages.when)) {
pendingIdleHandlerCount = mIdleHandlers.size();
}
if (pendingIdleHandlerCount <= 0) {
// No idle handlers to run. Loop and wait some more.
mBlocked = true;
continue;
}
if (mPendingIdleHandlers == null) {
mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
}
mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
}
// Run the idle handlers.
// We only ever reach this code block during the first iteration.
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler
boolean keep = false;
try {
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf(TAG, "IdleHandler threw exception", t);
}
if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}
// Reset the idle handler count to 0 so we do not run them again.
pendingIdleHandlerCount = 0;
// While calling an idle handler, a new message could have been delivered
// so go back and look again for a pending message without waiting.
nextPollTimeoutMillis = 0;
}
}
可以看到如果msg为null会标记nextPollTimeoutMillis = -1;
nativePollOnce(ptr, nextPollTimeoutMillis);这行代码就会使消息队列进入休眠状态
什么时候醒来呢?
在enqueueMessage中有这么一行代码
if (needWake) {
nativeWake(mPtr);
}
Looper拿到消息后就会通过msg.target.dispatchMessage(msg);将消息发送给这条消息应该被处理的Handler
在Handler的enqueueMessage中有这么一行代码
msg.target = this;
因此我们来看看Handler的dispatchMessage
/**
* Handle system messages here.
*/
public void dispatchMessage(@NonNull Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
msg.callback就是我们发送的Runnable,handleCallback时这样实现的
private static void handleCallback(Message message) {
message.callback.run();
}
如果msg.callback == null并且mCallback==null
就会调用handleMessage(msg) handleMessage是一个空方法需要我们去重写
那mCallback是什么东西?
mCallback是个接口,定义如下:
/**
* Callback interface you can use when instantiating a Handler to avoid
* having to implement your own subclass of Handler.
*/
public interface Callback {
/**
* @param msg A {@link android.os.Message Message} object
* @return True if no further handling is desired
*/
boolean handleMessage(@NonNull Message msg);
}
通过它我们可以通过Handler handler=new Handler(callback)来创建Handler,这样我们就可以不通过创建一个Handler并派生它的子类的方式并重写handleMessage的方法来实现Handler了
到这Handler分析的就差不多了
最后再来看看Looper怎么退出把
它有一个quit函数
public void quit() {
mQueue.quit(false);
}
可以看到调用了消息队列的quit函数并传入false
来看看
void quit(boolean safe) {
if (!mQuitAllowed) {
throw new IllegalStateException("Main thread not allowed to quit.");
}
synchronized (this) {
if (mQuitting) {
return;
}
mQuitting = true;
if (safe) {
removeAllFutureMessagesLocked();
} else {
removeAllMessagesLocked();
}
// We can assume mPtr != 0 because mQuitting was previously false.
nativeWake(mPtr);
}
}
会调用removeAllMessagesLocked(); 从名字就可以看出是清空消息队列中的所有消息
如果消息队列处于休眠状态会调用nativeWake(mPtr);这样在Looper的loop函数就会因为取出的消息为null而退出了!