/** * Starts the background initialization. With this method the initializer * becomes active and invokes the {@link #initialize()} method in a * background task. A {@code BackgroundInitializer} can be started exactly * once. The return value of this method determines whether the start was * successful: only the first invocation of this method returns <b>true</b>, * following invocations will return <b>false</b>. * * @return a flag whether the initializer could be started successfully */ public synchronized boolean start() { // Not yet started? if (!isStarted()) { // Determine the executor to use and whether a temporary one has to // be created ExecutorService tempExec; executor = getExternalExecutor(); if (executor == null) { executor = tempExec = createExecutor(); } else { tempExec = null; } future = executor.submit(createTask(tempExec)); return true; } return false; }
if (bi.getExternalExecutor() == null) { bi.setExternalExecutor(exec); bi.start(); for (final Map.Entry<String, BackgroundInitializer<?>> e : inits.entrySet()) { try { results.put(e.getKey(), e.getValue().get()); } catch (final ConcurrentException cex) { excepts.put(e.getKey(), cex);
/** * Returns the result of the background initialization. This method blocks * until initialization is complete. If the background processing caused a * runtime exception, it is directly thrown by this method. Checked * exceptions, including {@code InterruptedException} are wrapped in a * {@link ConcurrentException}. Calling this method before {@link #start()} * was called causes an {@code IllegalStateException} exception to be * thrown. * * @return the object produced by this initializer * @throws ConcurrentException if a checked exception occurred during * background processing * @throws IllegalStateException if {@link #start()} has not been called */ @Override public T get() throws ConcurrentException { try { return getFuture().get(); } catch (final ExecutionException execex) { ConcurrentUtils.handleCause(execex); return null; // should not be reached } catch (final InterruptedException iex) { // reset interrupted state Thread.currentThread().interrupt(); throw new ConcurrentException(iex); } }
/** * Creates the {@code ExecutorService} to be used. This method is called if * no {@code ExecutorService} was provided at construction time. * * @return the {@code ExecutorService} to be used */ private ExecutorService createExecutor() { return Executors.newFixedThreadPool(getTaskCount()); }
/** * Creates a new instance of {@code BackgroundInitializer} and initializes * it with the given {@code ExecutorService}. If the {@code ExecutorService} * is not null, the background task for initializing this object will be * scheduled at this service. Otherwise a new temporary {@code * ExecutorService} is created. * * @param exec an external {@code ExecutorService} to be used for task * execution */ protected BackgroundInitializer(final ExecutorService exec) { setExternalExecutor(exec); }
/** * Sets an {@code ExecutorService} to be used by this class. The {@code * ExecutorService} passed to this method is used for executing the * background task. Thus it is possible to re-use an already existing * {@code ExecutorService} or to use a specially configured one. If no * {@code ExecutorService} is set, this instance creates a temporary one and * destroys it after background initialization is complete. Note that this * method must be called before {@link #start()}; otherwise an exception is * thrown. * * @param externalExecutor the {@code ExecutorService} to be used * @throws IllegalStateException if this initializer has already been * started */ public final synchronized void setExternalExecutor( final ExecutorService externalExecutor) { if (isStarted()) { throw new IllegalStateException( "Cannot set ExecutorService after start()!"); } this.externalExecutor = externalExecutor; }
/** * Returns the number of tasks needed for executing all child {@code * BackgroundInitializer} objects in parallel. This implementation sums up * the required tasks for all child initializers (which is necessary if one * of the child initializers is itself a {@code MultiBackgroundInitializer} * ). Then it adds 1 for the control task that waits for the completion of * the children. * * @return the number of tasks required for background processing */ @Override protected int getTaskCount() { int result = 1; for (final BackgroundInitializer<?> bi : childInitializers.values()) { result += bi.getTaskCount(); } return result; }
/** * Creates a new instance of {@code BackgroundInitializer} and initializes * it with the given {@code ExecutorService}. If the {@code ExecutorService} * is not null, the background task for initializing this object will be * scheduled at this service. Otherwise a new temporary {@code * ExecutorService} is created. * * @param exec an external {@code ExecutorService} to be used for task * execution */ protected BackgroundInitializer(final ExecutorService exec) { setExternalExecutor(exec); }
/** * Sets an {@code ExecutorService} to be used by this class. The {@code * ExecutorService} passed to this method is used for executing the * background task. Thus it is possible to re-use an already existing * {@code ExecutorService} or to use a specially configured one. If no * {@code ExecutorService} is set, this instance creates a temporary one and * destroys it after background initialization is complete. Note that this * method must be called before {@link #start()}; otherwise an exception is * thrown. * * @param externalExecutor the {@code ExecutorService} to be used * @throws IllegalStateException if this initializer has already been * started */ public final synchronized void setExternalExecutor( final ExecutorService externalExecutor) { if (isStarted()) { throw new IllegalStateException( "Cannot set ExecutorService after start()!"); } this.externalExecutor = externalExecutor; }
/** * Starts the background initialization. With this method the initializer * becomes active and invokes the {@link #initialize()} method in a * background task. A {@code BackgroundInitializer} can be started exactly * once. The return value of this method determines whether the start was * successful: only the first invocation of this method returns <b>true</b>, * following invocations will return <b>false</b>. * * @return a flag whether the initializer could be started successfully */ public synchronized boolean start() { // Not yet started? if (!isStarted()) { // Determine the executor to use and whether a temporary one has to // be created ExecutorService tempExec; executor = getExternalExecutor(); if (executor == null) { executor = tempExec = createExecutor(); } else { tempExec = null; } future = executor.submit(createTask(tempExec)); return true; } return false; }
if (bi.getExternalExecutor() == null) { bi.setExternalExecutor(exec); bi.start(); for (final Map.Entry<String, BackgroundInitializer<?>> e : inits.entrySet()) { try { results.put(e.getKey(), e.getValue().get()); } catch (final ConcurrentException cex) { excepts.put(e.getKey(), cex);
/** * Creates the {@code ExecutorService} to be used. This method is called if * no {@code ExecutorService} was provided at construction time. * * @return the {@code ExecutorService} to be used */ private ExecutorService createExecutor() { return Executors.newFixedThreadPool(getTaskCount()); }
/** * Creates a new instance of {@code BackgroundInitializer} and initializes * it with the given {@code ExecutorService}. If the {@code ExecutorService} * is not null, the background task for initializing this object will be * scheduled at this service. Otherwise a new temporary {@code * ExecutorService} is created. * * @param exec an external {@code ExecutorService} to be used for task * execution */ protected BackgroundInitializer(final ExecutorService exec) { setExternalExecutor(exec); }
/** * Sets an {@code ExecutorService} to be used by this class. The {@code * ExecutorService} passed to this method is used for executing the * background task. Thus it is possible to re-use an already existing * {@code ExecutorService} or to use a specially configured one. If no * {@code ExecutorService} is set, this instance creates a temporary one and * destroys it after background initialization is complete. Note that this * method must be called before {@link #start()}; otherwise an exception is * thrown. * * @param externalExecutor the {@code ExecutorService} to be used * @throws IllegalStateException if this initializer has already been * started */ public final synchronized void setExternalExecutor( final ExecutorService externalExecutor) { if (isStarted()) { throw new IllegalStateException( "Cannot set ExecutorService after start()!"); } this.externalExecutor = externalExecutor; }
/** * Returns the result of the background initialization. This method blocks * until initialization is complete. If the background processing caused a * runtime exception, it is directly thrown by this method. Checked * exceptions, including {@code InterruptedException} are wrapped in a * {@link ConcurrentException}. Calling this method before {@link #start()} * was called causes an {@code IllegalStateException} exception to be * thrown. * * @return the object produced by this initializer * @throws ConcurrentException if a checked exception occurred during * background processing * @throws IllegalStateException if {@link #start()} has not been called */ @Override public T get() throws ConcurrentException { try { return getFuture().get(); } catch (final ExecutionException execex) { ConcurrentUtils.handleCause(execex); return null; // should not be reached } catch (final InterruptedException iex) { // reset interrupted state Thread.currentThread().interrupt(); throw new ConcurrentException(iex); } }
/** * Starts the background initialization. With this method the initializer * becomes active and invokes the {@link #initialize()} method in a * background task. A {@code BackgroundInitializer} can be started exactly * once. The return value of this method determines whether the start was * successful: only the first invocation of this method returns <b>true</b>, * following invocations will return <b>false</b>. * * @return a flag whether the initializer could be started successfully */ public synchronized boolean start() { // Not yet started? if (!isStarted()) { // Determine the executor to use and whether a temporary one has to // be created ExecutorService tempExec; executor = getExternalExecutor(); if (executor == null) { executor = tempExec = createExecutor(); } else { tempExec = null; } future = executor.submit(createTask(tempExec)); return true; } return false; }
if (bi.getExternalExecutor() == null) { bi.setExternalExecutor(exec); bi.start(); for (final Map.Entry<String, BackgroundInitializer<?>> e : inits.entrySet()) { try { results.put(e.getKey(), e.getValue().get()); } catch (final ConcurrentException cex) { excepts.put(e.getKey(), cex);
/** * Creates the {@code ExecutorService} to be used. This method is called if * no {@code ExecutorService} was provided at construction time. * * @return the {@code ExecutorService} to be used */ private ExecutorService createExecutor() { return Executors.newFixedThreadPool(getTaskCount()); }
/** * Creates a new instance of {@code BackgroundInitializer} and initializes * it with the given {@code ExecutorService}. If the {@code ExecutorService} * is not null, the background task for initializing this object will be * scheduled at this service. Otherwise a new temporary {@code * ExecutorService} is created. * * @param exec an external {@code ExecutorService} to be used for task * execution */ protected BackgroundInitializer(final ExecutorService exec) { setExternalExecutor(exec); }
/** * Sets an {@code ExecutorService} to be used by this class. The {@code * ExecutorService} passed to this method is used for executing the * background task. Thus it is possible to re-use an already existing * {@code ExecutorService} or to use a specially configured one. If no * {@code ExecutorService} is set, this instance creates a temporary one and * destroys it after background initialization is complete. Note that this * method must be called before {@link #start()}; otherwise an exception is * thrown. * * @param externalExecutor the {@code ExecutorService} to be used * @throws IllegalStateException if this initializer has already been * started */ public final synchronized void setExternalExecutor( final ExecutorService externalExecutor) { if (isStarted()) { throw new IllegalStateException( "Cannot set ExecutorService after start()!"); } this.externalExecutor = externalExecutor; }