Handler.java
import java.util.Queue;
public class Handler {
Looper mLooper;
MessageQueue mQueue;
//
public Handler(){
//
mLooper = Looper.myLooper();
mQueue = mLooper.mQueue;
Log.log("mLooper = "+mLooper);
}
public void sendMessage(Message msg){
msg.target = this;
mQueue.add(msg);
}
public void handMessage(Message message){
}
}
Looper.java
public class Looper {
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<>();
static Looper sMainLooper;
MessageQueue mQueue ;
Thread mThread;
public Looper(){
mQueue = new MessageQueue();
mThread = Thread.currentThread();
}
public static void prepare(){
//不能准备两次
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
//为线程绑定一个Looper 这个looper 可在多线程中共享
Looper looper = new Looper();
sThreadLocal.set(looper);
}
//looper
public static Looper myLooper(){
return sThreadLocal.get();
}
public static void looper(){
Looper looper = Looper.myLooper();
while (true){
//无限循环
MessageQueue queue = looper.mQueue;
Message message = queue.next();
if (message!=null){
message.target.handMessage(message);
}
}
}
//这个函数只在main当中调用一次
public static void prepareMainLooper() {
prepare();
synchronized (Looper.class) {
if (sMainLooper!=null){
throw new IllegalStateException("The main Looper has already been prepared.");
}
sMainLooper = myLooper();
}
}
}
Message.java
public class Message {
Message pre;
Message next;
Handler target;
private String messageId;
private String message;
public static Message obtain( String messageId , String msg){
Message message = new Message();
message.message = msg;
message.messageId = messageId;
return message;
}
@Override
public String toString() {
return "messageId="+messageId + "message="+message;
}
}
MessageQueue.java
public class MessageQueue {
Message message;
boolean isBlock = false;
public MessageQueue(){
}
public Message next(){
while (true){
synchronized (this){
if (message != null){
isBlock = false;
Message msg = this.message;
this.message = this.message.next;
return msg;
}
if (message == null){
Log.log("没有消息");
isBlock = true;
}
try {
this.wait(16);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void add(Message msg){
synchronized (this){
if (message == null){
this.message = msg;
}else {
Message p = message;
while (p.next != null){
p = p.next;
}
p.next = msg;
}
}
}
}
Log.java
public class Log {
public static void log(String msg){
System.out.println(msg);
}
}
UI.java 测试用的
public class UI {
public static void main(String[] s){
Looper.prepareMainLooper();
Handler handler = new Handler(){
@Override
public void handMessage(Message message) {
super.handMessage(message);
Log.log("time = "+System.currentTimeMillis() + " Thread " + Thread.currentThread().getName()+ " 我收到消息 message " + message.toString());
}
};
handler.sendMessage(Message.obtain("1" , "string1"));
//模拟发消息线程 t1
Thread t1 = new Thread(new Runnable() {
int messageId = 0;
@Override
public void run() {
while (true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message = Message.obtain(messageId +"", "来自线程t1");
handler.sendMessage(message);
messageId++;
}
}
});
t1.setName("t1");
t1.start();
//模拟发消息线程 t2
Thread t2 = new Thread(new Runnable() {
int messageId = 0;
@Override
public void run() {
while (true){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
//一次发10条
for (int i = 0; i < 10; i++) {
Message message = Message.obtain(messageId +" ", "来自线程t2");
handler.sendMessage(message);
messageId++;
}
}
}
});
t2.setName("t1");
t2.start();
Looper.looper();
}
}