java 可以使用v7.1 UI.access()方法执行后台任务以在Vaadin中执行后台工作和安全UI更新。目前

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 可以使用v7.1 UI.access()方法执行后台任务以在Vaadin中执行后台工作和安全UI更新。目前相关的知识,希望对你有一定的参考价值。

package org.mpilone.vaadin;

import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;

import com.vaadin.server.VaadinSession;
import com.vaadin.ui.UI;
import com.vaadin.ui.UIDetachedException;

/**
 * A background task that handles synchronizing to the UI when performing
 * updates from a background thread. The common use case is to create a
 * background task, add it to a thread pool and then update the UI in the safe,
 * {@link #doUiUpdate(Throwable)} method.
 * 
 * @author mpilone
 */
public abstract class BackgroundUiTask implements RunnableFuture<Void> {

  /**
   * Called when a lock has been obtained on the UI's {@link VaadinSession} and
   * it is safe to apply UI updates. This method will only be called after
   * {@link #doWork()} is complete and the task hasn't been cancelled. The
   * default implementation calls {@link #doUiUpdate()}. This method will be
   * called even if {@link #doWork()} throws an exception to give the task a
   * chance to cleanup the UI.
   * 
   * @param ex
   *          the exception raised in {@link #doWork()} or null if no exception
   *          was raised
   */
  protected void doUiUpdate(Throwable ex) {
    doUiUpdate();
  }

  /**
   * A convenience method that will be called by {@link #doUiUpdate(Throwable)}.
   * The default implementation does nothing but subclasses can override this
   * method if the value of the exception is not important.
   */
  protected void doUiUpdate() {
  }

  /**
   * Called when the task is started. All time consuming work should be done in
   * this method. No UI updates are permitted in this method because it is not
   * synchronized to the {@link VaadinServiceSession}. Good implementations
   * should attempt to stop if the task is cancelled while processing.
   */
  protected abstract void doWork();

  /**
   * A simple runnable that executes the first of two parts of a
   * {@link BackgroundUiTask}. The {@link BackgroundUiTask#doWork()} method will
   * be called and the {@link BackgroundUiTask#doneLatch} will be decremented.
   * 
   * @author mpilone
   */
  private class DoWorkRunnable implements Runnable {
    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
      try {
        if (ui == null) {
          throw new UIDetachedException("No UI available for "
              + "background synchronization.");
        }

        doWork();
      }
      finally {
        doneLatch.countDown();
      }
    }
  }

  /**
   * A simple runnable that executes the second of two parts of a
   * {@link BackgroundUiTask}. The
   * {@link BackgroundUiTask#doUiUpdate(Throwable)} method will be called and
   * the {@link BackgroundUiTask#doneLatch} will be decremented.
   * 
   * @author mpilone
   */
  private class DoUiUpdateRunnable implements Runnable {
    private Throwable exception;

    public DoUiUpdateRunnable(Throwable exception) {
      this.exception = exception;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
      try {
        if (ui == null) {
          throw new UIDetachedException("No UI available for "
              + "background synchronization.");
        }

        doUiUpdate(exception);
      }
      finally {
        doneLatch.countDown();
      }
    }
  }

  /**
   * The mutex for swapping futures to ensure that the active future is safely
   * accessed during all delegated calls.
   */
  private final Object FUTURE_MUTEX = new Object();

  /**
   * The active future that is running/will be run. When the future is complete,
   * it must count down the {@link #doneLatch}.
   */
  private Future<Void> future;

  /**
   * The count down latch used to keep track of the number of futures that need
   * to execute before this task is considered complete. Calls to {@link #get()}
   * will block on this latch until it reaches 0.
   */
  private CountDownLatch doneLatch = new CountDownLatch(2);

  /**
   * The UI to synchronize with when updating from a background thread.
   */
  private UI ui;

  /**
   * Constructs the background task which will synchronized with the UI
   * available at {@link UI#getCurrent()}. The task must be initially
   * constructed in the main thread where the UI is available.
   */
  public BackgroundUiTask() {
    this(UI.getCurrent());
  }

  /**
   * Constructs the background task which will synchronized with the given UI.
   */
  public BackgroundUiTask(UI ui) {
    this.ui = ui;

    // We wrap the runnable in a future task to get a common API to which to
    // delegate. The task also handles all the tricky exception handling and
    // thread safe cancellation.
    this.future = new FutureTask<Void>(new DoWorkRunnable(), null);
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.util.concurrent.Future#cancel(boolean)
   */
  @Override
  public boolean cancel(boolean mayInterruptIfRunning) {
    synchronized (FUTURE_MUTEX) {
      return future.cancel(mayInterruptIfRunning);
    }
  }

  /**
   * A convenience method for {@link #cancel(boolean)} with a value of false.
   */
  public void cancel() {
    cancel(false);
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.util.concurrent.Future#get()
   */
  @Override
  public Void get() throws InterruptedException, ExecutionException {
    doneLatch.await();
    return future.get();
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit)
   */
  @Override
  public Void get(long timeout, TimeUnit unit) throws InterruptedException,
      ExecutionException, TimeoutException {

    if (doneLatch.await(timeout, unit)) {
      return future.get(timeout, unit);
    }
    else {
      throw new TimeoutException();
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.util.concurrent.Future#isCancelled()
   */
  @Override
  public boolean isCancelled() {
    synchronized (FUTURE_MUTEX) {
      return future.isCancelled();
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.util.concurrent.Future#isDone()
   */
  @Override
  public boolean isDone() {
    return doneLatch.getCount() == 0 && future.isDone();
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.util.concurrent.RunnableFuture#run()
   */
  @Override
  public void run() {

    // Run the initial future, the doWork runnable.
    ((FutureTask<Void>) future).run();

    synchronized (FUTURE_MUTEX) {
      Throwable exception = null;
      try {
        // Check if we got an exception during execution.
        future.get();
      }
      catch (Throwable ex) {
        exception = ex;
      }

      if (future.isCancelled()) {
        // Cancelled during doWork so we'll skip doUiUpdate and simply count
        // down.
        doneLatch.countDown();
      }
      else {
        // Fire up doUiUpdate runnable which gets us the second future of the
        // task.
        future = ui.access(new DoUiUpdateRunnable(exception));
      }
    }
  }
}

以上是关于java 可以使用v7.1 UI.access()方法执行后台任务以在Vaadin中执行后台工作和安全UI更新。目前的主要内容,如果未能解决你的问题,请参考以下文章

WebSphere MQ v7.1 安全用户凭证

Win 10 + IIS + PHP v7.1.7 + SQL Server 2008 R2如何让SQL Server连接正常工作?

在 Mobilefirst V7.1 上使用 Clevertap 推送通知在启动画面上出现 iOS 应用程序崩溃问题

匿名上位机v2.6和V7自定义帧代码和飞控姿态代码

漏洞复现-致远OA

Arduino I/O Expansion Shield V7.1