小编Wil*_*ill的帖子

我是否需要同步对仅由一个线程修改的List的访问?

这里我有一个类,它有两个可以访问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)

java multithreading paint thread-synchronization

9
推荐指数
1
解决办法
139
查看次数

如何在EditText视图的焦点上默认使用数字小键盘?

我有一个EditText字段供用户输入以磅为单位的重量.如何覆盖传统键盘并显示数字键盘?

android android-widget

2
推荐指数
1
解决办法
5029
查看次数