-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
40 lines (36 loc) · 1.28 KB
/
Copy pathMain.java
File metadata and controls
40 lines (36 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.UUID;
public class Main {
public static void main(String[] args) {
// 初始化Looper
Looper.prepare();
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println(Thread.currentThread().getName() + "--receiver--" + msg.toString());
}
};
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
public void run() {
while (true) {
Message msg = new Message();
msg.what = 0;
synchronized (UUID.class) {
msg.object = Thread.currentThread().getName()+"--send---"+UUID.randomUUID().toString();
}
System.out.println(msg);
handler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
//开始消息循环
Looper.loop();
}
}