InfiniteLoop.java revision 2646:4c12464a907d
1139804Simp/*
21541Srgrimes * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
31541Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41541Srgrimes *
51541Srgrimes * This code is free software; you can redistribute it and/or modify it
61541Srgrimes * under the terms of the GNU General Public License version 2 only, as
71541Srgrimes * published by the Free Software Foundation.
81541Srgrimes *
91541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131541Srgrimes * accompanied this code).
141541Srgrimes *
151541Srgrimes * You should have received a copy of the GNU General Public License version
161541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181541Srgrimes *
191541Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201541Srgrimes * or visit www.oracle.com if you need additional information or have any
211541Srgrimes * questions.
221541Srgrimes */
231541Srgrimes
241541Srgrimespackage jdk.test.lib;
251541Srgrimes
261541Srgrimesimport java.util.Objects;
271541Srgrimes
281541Srgrimes/**
291541Srgrimes * Class which runs another Runnable in infinite loop with certain pauses
301541Srgrimes * between cycles.
311541Srgrimes */
321541Srgrimespublic class InfiniteLoop implements Runnable {
331541Srgrimes    private final Runnable target;
341541Srgrimes    private final long mills;
351541Srgrimes
361541Srgrimes
37116182Sobrien    /**
38116182Sobrien     * @param target a target to run in a loop
39116182Sobrien     * @param mills  the length of pause time in milliseconds
40224778Srwatson     * @throws NullPointerException if target is null
41190759Srwatson     * @throws IllegalArgumentException if the value of millis is negative
4213203Swollman     */
4313203Swollman    public InfiniteLoop(Runnable target, long mills) {
441541Srgrimes        Objects.requireNonNull(target);
452112Swollman        if (mills < 0) {
4669664Speter            throw new IllegalArgumentException("mills < 0");
47224778Srwatson        }
48177785Skib        this.target = target;
49192895Sjamie        this.mills = mills;
5076166Smarkm    }
5189316Salfred
521541Srgrimes    @Override
531541Srgrimes    public void run() {
541541Srgrimes        try {
551541Srgrimes            while (true) {
561541Srgrimes                target.run();
57190759Srwatson                if (mills > 0) {
58141471Sjhb                    Thread.sleep(mills);
59144613Sjeff                }
601541Srgrimes            }
611541Srgrimes        } catch (InterruptedException e) {
621541Srgrimes            Thread.currentThread().interrupt();
631541Srgrimes            throw new Error(e);
64155334Srwatson        }
65163606Srwatson    }
66155334Srwatson}
6792751Sjeff