这里我有一个类,它有两个可以访问List的线程.一个线程定期用更新的副本替换列表,另一个线程将列表的内容绘制到屏幕上.
public class ThreadSafePainter {
private List<String> dataList = new ArrayList<>();
/*
* starts a thread to periodically update the dataList
*/
public ThreadSafePainter() {
Thread thread = new Thread(() -> {
while (true) {
// replace out-dated list with the updated data
this.dataList = getUpdatedData();
// wait a few seconds before updating again
Thread.sleep(5000);
}
});
thread.start();
}
/*
* called 10 times/second from a separate paint thread
* Q: Does access to dataList need to be synchronized?
*/ …Run Code Online (Sandbox Code Playgroud) 我有一个EditText字段供用户输入以磅为单位的重量.如何覆盖传统键盘并显示数字键盘?