【问题标题】:vaadin table container concurrent modificationvaadin 表容器并发修改
【发布时间】:2014-06-05 16:45:13
【问题描述】:

在我基于 vaadin 的 Web 应用程序中,我有以下两个类。在 MyTable 类中,realTimeUpdate 方法每秒不断更新表值。同时从用户操作方法 1 被调用以进行表行编辑。当用户操作调用 method1 时,会发生“并发修改异常”。我可以为这个问题申请的最佳解决方案是什么?

public class MyView extends CustomComponent implements View{
    private IndexedContainer container = new IndexedContainer();
    private Table table = new Table();
    public MyView(){
        ......
        table.setContainerDataSource(container);
    }
}

public class MyTable
{
   public void method1(Table table)
   {
      Container container = table.getContainerDataSource();
      Iterator<?> iterator = container.getItemIds().iterator();
      while (iterator.hasNext())
      {
        Object itemId = iterator.next();
        ...... some code to modify table row
      }
   }

   public void realTimeUpdate(Table table)
   {
     Container container = table.getContainerDataSource();
     Iterator<?> iterator = container.getItemIds().iterator();
     while (iterator.hasNext()) 
     {
        Object itemId = iterator.next();
        ...... some code to update table values
     }
    }
}

【问题讨论】:

    标签: java vaadin vaadin7 concurrentmodification


    【解决方案1】:

    Vaadin 期望对它的访问被正确锁定。所以当你有一个外部线程修改 Vaadin 组件或其他 Vaadin 相关类时,你必须使用 UI.access 来确保锁定。编写你的 realTimeUpdate 方法如下:

    public void realTimeUpdate(Table table) {
         table.getUI().access(new Runnable() {
             @Override
             public void run() {
                 Container container = table.getContainerDataSource();
                 Iterator<?> iterator = container.getItemIds().iterator();
                 while (iterator.hasNext()) 
                 {
                     Object itemId = iterator.next();
                     ...... some code to update table values
                 }
             }
        });
    }
    

    【讨论】:

    • 这是在 Vaadin 中获取锁的首选方式,但如果您需要更精细的控制,也可以使用 VaadinSession.lock()
    • @Swarne27,你应该接受这个答案。它解决了我的类似问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2013-09-23
    相关资源
    最近更新 更多