Skip to content

Commit 892dbbd

Browse files
committed
refactor
1 parent 41d03e5 commit 892dbbd

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

Simple_eventbus_demo/src/org/simple/eventbus/demo/fragment/MenuFragment.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.simple.eventbus.demo.R;
4040
import org.simple.eventbus.demo.bean.Person;
4141

42-
import java.util.Date;
4342
import java.util.Random;
4443

4544
/**
@@ -49,6 +48,8 @@ public class MenuFragment extends Fragment {
4948

5049
public static final String CLICK_TAG = "click_user";
5150

51+
PostThread[] threads = new PostThread[4];
52+
5253
TextView mUserNameTv;
5354
TextView mTimerTv;
5455

@@ -93,27 +94,14 @@ public void onClick(View v) {
9394
});
9495

9596
for (int i = 0; i < 4; i++) {
96-
threads[i] = new TimerThread(i);
97+
threads[i] = new PostThread(i);
9798
threads[i].start();
9899
}
99100

100101
EventBus.getDefault().register(this);
101102
return rootView;
102103
}
103104

104-
TimerThread[] threads = new TimerThread[4];
105-
106-
@Override
107-
public void onDestroy() {
108-
109-
for (TimerThread timerThread : threads) {
110-
timerThread.interrupt();
111-
}
112-
113-
EventBus.getDefault().unregister(this);
114-
super.onDestroy();
115-
}
116-
117105
@Subcriber(tag = CLICK_TAG)
118106
private void updateClickUserName(Person clickPerson) {
119107
mUserNameTv.setText(clickPerson.name);
@@ -123,28 +111,40 @@ private void updateClickUserName(Person clickPerson) {
123111
* 模拟从异步线程发来的更新信息
124112
*/
125113
@Subcriber
126-
private void updateTime(String time) {
114+
private void updateTime(String name) {
127115
Log.e(getTag(), "### update time, thread = " + Thread.currentThread().getName());
128-
mTimerTv.setText(time + " , " + new Date().toGMTString());
116+
117+
// 从哪个线程投递来的消息
118+
mTimerTv.setText("from " + name);
129119

130120
// post 给TimerThread线程
131121
EventBus.getDefault().post("I am tom, ", "sayhello");
132122
}
133123

134124
@Subcriber(mode = ThreadMode.POST)
135125
private void invokeInPostThread(String event) {
136-
Log.e(getTag(), "### invokeInPostThread invoked, thread = "
126+
Log.e(getTag(), "### invokeInPostThread invoke in thread = "
137127
+ Thread.currentThread().getName());
138128
}
139129

130+
@Override
131+
public void onDestroy() {
132+
for (PostThread timerThread : threads) {
133+
timerThread.interrupt();
134+
}
135+
136+
EventBus.getDefault().unregister(this);
137+
super.onDestroy();
138+
}
139+
140140
/**
141141
* @author mrsimple
142142
*/
143-
class TimerThread extends Thread {
143+
class PostThread extends Thread {
144144

145145
int mIndex;
146146

147-
public TimerThread(int index) {
147+
public PostThread(int index) {
148148
mIndex = index;
149149
setName("TimerThread - " + index);
150150
EventBus.getDefault().register(this);
@@ -161,8 +161,7 @@ private void hello(String name) {
161161
@Override
162162
public void run() {
163163
Log.e(getTag(), "### queue : " + EventBus.getDefault().getEventQueue().hashCode()
164-
+ ", bus = "
165-
+ EventBus.getDefault());
164+
+ ", bus = " + EventBus.getDefault());
166165

167166
while (!this.isInterrupted()) {
168167
EventBus.getDefault().post(getName());

0 commit comments

Comments
 (0)