java - 识别能够更新 UI 组件的线程

标签 java multithreading vaadin vaadin7

我在网上搜索了一些提示,但一无所获。也许我搜索错了。

我的问题是:

如何确定当前正在执行的线程是否有资格修改 UI 组件?

这是我现在为 UI 更新所做的:

UI.getCurrent().access(new Runnable() {
            @Override
            public void run() {
               //modify some ui
            }
        });

在某些情况下,我不必这样做,它会在当前线程中立即运行,但有时不会,并且我会收到错误消息。 当我比较 Runnable 中的线程和 Runnable 外部的线程时 - 这是完全相同的东西 = http-bio-8080-exec-10

我怎样才能从代码中知道这是调用 UI.getCurrent().access() 的正确位置还是立即调用?

我尝试搜索线程中的差异,查找:VaadinService.getCurrentResponse()VaadinService.getCurrentRequest(),但我没有看到任何规则或模式。

感谢您的宝贵时间, 托马斯

最佳答案

我从 Vaadin 开发人员那里得到了答案:

Johannes Dahlström 写道:

If in any doubt, use UI.access - it's never wrong.

Basically, the only place where you don't need it (or some other means to lock the session) is when you know you're in a thread that's currently handling a client request (for instance, in a component event listener). In cases you don't know whether this is the case, for instance in a method that could be called either from an event listener or a background worker thread, you should either do UI.access just to be sure, or alternatively simply access the UI directly and document in the method's contract that its caller must take care of locking.

来源:https://vaadin.com/forum#!/thread/4479235/4491237

关于java - 识别能够更新 UI 组件的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20264152/

相关文章:

Python 哪个线程先启动?

java - 无法插入组合框

java - 客户端重启后如何重新获取InterProcessMutex

java - 二叉树的中序迭代器 - java

java - 正则表达式 Java,为什么这个正则表达式这么慢?

python - 在 Python 中决定子进程、多处理和线程?

java - Reducer 在 mapper 完成之前启动

matlab - 如何在 MATLAB 中进行线程化?

java - 如何在 Vaadin 8 中添加 validator ?

java - 如何对 ComboBox 项目进行排序