This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.examples.pool;
020
021import org.apache.reef.tang.annotations.Parameter;
022import org.apache.reef.task.Task;
023
024import javax.inject.Inject;
025import java.util.logging.Level;
026import java.util.logging.Logger;
027
028/**
029 * Sleep for delay seconds and quit.
030 */
031public final class SleepTask implements Task {
032
033  /**
034   * Standard java logger.
035   */
036  private static final Logger LOG = Logger.getLogger(SleepTask.class.getName());
037
038  /**
039   * Number of milliseconds to sleep.
040   */
041  private final int delay;
042
043  /**
044   * Task constructor. Parameters are injected automatically by TANG.
045   *
046   * @param delay number of seconds to sleep.
047   */
048  @Inject
049  private SleepTask(@Parameter(Launch.Delay.class) final Integer delay) {
050    this.delay = delay * 1000;
051  }
052
053  /**
054   * Sleep for delay milliseconds and return.
055   *
056   * @param memento ignored.
057   * @return null.
058   */
059  @Override
060  public byte[] call(final byte[] memento) {
061    LOG.log(Level.FINE, "Task started: sleep for: {0} msec.", this.delay);
062    final long ts = System.currentTimeMillis();
063    for (long period = this.delay; period > 0; period -= System.currentTimeMillis() - ts) {
064      try {
065        Thread.sleep(period);
066      } catch (final InterruptedException ex) {
067        LOG.log(Level.FINEST, "Interrupted: {0}", ex);
068      }
069    }
070    LOG.log(Level.FINE, "Task finished after {0} msec.", System.currentTimeMillis() - ts);
071    return null;
072  }
073}