java - Threads in swing freezing - java
问题描述
I'd like to realize a fake bash where when i append multiple strings on my output (JTextPane), there is a little wait between a message and another. I did it with a thread, but all freeze when I append a new message. This is the code for append:
public void append(String s) {
Thread t = new Thread(){
public void run() {
textPane.setText(textPane.getText()+"\n"+s);
try {
Thread.sleep((long) (Math.random()%1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
But the result is that all freeze waiting all messages is written. What can I do?
解决方案
推荐阅读
- java - 如何从多个线程更新缓存
- scala - Spark过滤器+地图与flatMap
- jenkins - 从阶段级别更新 Jenkins 管道中的环境全局变量 - 有可能吗?
- javascript - 如何将值打包为带符号的 32 int
- android - 当 onDestroy 在 android 中运行时 MainActivity 泄漏了 ServiceConnection。如何防止这个错误?
- node.js - WebStorm 建议我翻转 if-else,这样做有什么好处?
- c++ - 为什么 C++ 中的内联类成员函数会导致符号未找到错误?
- java - 在 Fragment 中重用 webView 等单例视图
- c# - C# UWP - 如何将移动模拟器连接到本地 MySQL 数据库?
- r - 如何在R中按范围合并两个数据帧?