vaadin表容器并发修改

时间:2014-06-05 16:45:13

标签: java vaadin vaadin7 concurrentmodification

在我的基于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
     }
    }
}

1 个答案:

答案 0 :(得分:2)

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
             }
         }
    });
}