我有以下课程。该类的目的是允许我通过每秒显示大约十个字符来模拟电传打字机/打字机。
CharacterLoopThread类的目的是查看outputBuffer,如果其中有任何字符,则在UI线程上调用runnable,该线程将第一个字符拉出并将其插入textView。然后线程休眠约100ms。 (这里有一些恶作剧......虽然我在1979年使用它时电传打字很惊人,现在我的口味有点慢。所以每10个字符,我会稍微减少延迟。当没有更多的字符显示时,我将延迟重置为100毫秒......)
我编辑了课程的底部,因为它与我的问题没有密切关系。
我在这里看起来效果很好。但是,它是因为我还是不管我的?编写线程和处理程序的首选方法是什么?
public class MyActivity extends Activity {
private TextView textView;
private ScrollView scrollView;
private StringBuilder outputBuffer;
private Handler handler;
private CharacterLooperThread characterLooperThread;
(剪断)
private class CharacterLooperThread extends Thread {
private boolean allowRun;
private Runnable run;
int effectiveCharacterDelay;
int characterCount;
public CharacterLooperThread() {
allowRun = true;
run = new Runnable() {
public void run() {
/**
* Don't do anything if the string has been consumed. This is necessary since when the delay
* is very small it is possible for a runnable to be queued before the previous runnable has
* consumed the final character from the outputBuffer. The 2nd runnable will cause an
* exception on the substring() below.
*/
if (outputBuffer.length() == 0) return;
try {
textView.append(outputBuffer.substring(0, 1));
scrollToBottom();
outputBuffer.deleteCharAt(0);
} catch (Exception e) {
toast(getMsg(e));
}
}
};
}
public void run() {
resetDelay();
while (allowRun) {
/**
* This if() performs 2 functions:
* 1. It prevents us from queuing useless runnables in the handler. Why use the resources if
* there's nothing to display?
* 2. It allows us to reset the delay values. If the outputBuffer is depleted we can reset the
* delay to the starting value.
*/
if (outputBuffer.length() > 0) {
handler.post(run);
reduceDelay();
} else {
resetDelay();
}
try {
Thread.sleep(effectiveCharacterDelay);
} catch (InterruptedException e) {
toast("sleep() failed with " + e.getMessage());
}
}
/**
* Make sure there's no runnable on the queue when the thread exits.
*/
handler.removeCallbacks(run);
}
public void exit() {
allowRun = false;
}
答案 0 :(得分:1)
一个想法是使用Handler.postDelayed
来安排个人“击键”。您可以一次完成所有操作,也可以让每个按键Runnable
在完成后安排下一个按键;如果处理落后于计划,前一种方法将尽快赶上,而后者将基本上推回一切以保持相同的键间延迟。
我担心看到一个线程改变StringBuilder
而另一个线程读取它。 (StringBuilder
类是StringBuffer
的非线程安全继承者,这是在人们认为使单个类线程安全的设计好的那天写回来的。如果它偶尔不会出现意想不到的事情,那么你很幸运,尽管这里不会出错。但是,使用postDelayed
,您可能会完全摆脱后台线程。
只要您创建匿名Runnable
类,请注意您可以为它们提供参数(只要您声明变量final
)。因此,我倾向于一次向每个Runnable
发布一个字符,如下所示:
long delay = 0;
for (int j = 0; j < outputBuffer.length(); ++j) {
final CharSequence s = outputBuffer.subSequence(j, j + 1);
handler.postDelayed(new Runnable() {
@Override public void run() {
textView.append(s);
scrollToBottom();
}
}, delay);
delay += 100; // or whatever
}