1 package org.apache.maven.surefire.junitcore.pc; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.junit.runners.model.RunnerScheduler; 23 24 import java.util.concurrent.RejectedExecutionException; 25 26 /** 27 * Specifies the strategy of scheduling whether sequential, or parallel. 28 * The strategy may use a thread pool <em>shared</em> with other strategies. 29 * <p/> 30 * One instance of strategy can be used just by one {@link Scheduler}. 31 * <p/> 32 * The strategy is scheduling tasks in {@link #schedule(Runnable)} and awaiting them 33 * completed in {@link #finished()}. Both methods should be used in one thread. 34 * 35 * @author Tibor Digana (tibor17) 36 * @since 2.16 37 */ 38 public abstract class SchedulingStrategy { 39 40 /** 41 * Schedules tasks if {@link #canSchedule()}. 42 * 43 * @param task runnable to schedule in a thread pool or invoke 44 * @throws RejectedExecutionException if <tt>task</tt> 45 * cannot be scheduled for execution 46 * @throws NullPointerException if <tt>task</tt> is <tt>null</tt> 47 * @see RunnerScheduler#schedule(Runnable) 48 * @see java.util.concurrent.Executor#execute(Runnable) 49 */ 50 protected abstract void schedule(Runnable task); 51 52 /** 53 * Waiting for scheduled tasks to finish. 54 * New tasks will not be scheduled by calling this method. 55 * 56 * @return <tt>true</tt> if successfully stopped the scheduler, else 57 * <tt>false</tt> if already stopped (a <em>shared</em> thread 58 * pool was shutdown externally). 59 * @throws InterruptedException if interrupted while waiting 60 * for scheduled tasks to finish 61 * @see RunnerScheduler#finished() 62 */ 63 protected abstract boolean finished() throws InterruptedException; 64 65 /** 66 * Stops scheduling new tasks (e.g. by {@link java.util.concurrent.ExecutorService#shutdown()} 67 * on a private thread pool which cannot be <em>shared</em> with other strategy). 68 * 69 * @return <tt>true</tt> if successfully stopped the scheduler, else 70 * <tt>false</tt> if already stopped (a <em>shared</em> thread 71 * pool was shutdown externally). 72 * @see java.util.concurrent.ExecutorService#shutdown() 73 */ 74 protected abstract boolean stop(); 75 76 /** 77 * Stops scheduling new tasks and <em>interrupts</em> running tasks 78 * (e.g. by {@link java.util.concurrent.ExecutorService#shutdownNow()} on a private thread pool 79 * which cannot be <em>shared</em> with other strategy). 80 * <p> 81 * This method calls {@link #stop()} by default. 82 * 83 * @return <tt>true</tt> if successfully stopped the scheduler, else 84 * <tt>false</tt> if already stopped (a <em>shared</em> thread 85 * pool was shutdown externally). 86 * @see java.util.concurrent.ExecutorService#shutdownNow() 87 */ 88 protected boolean stopNow() { 89 return stop(); 90 } 91 92 protected void setDefaultShutdownHandler(Scheduler.ShutdownHandler handler) { 93 } 94 95 /** 96 * @return <tt>true</tt> if a thread pool associated with this strategy 97 * can be shared with other strategies. 98 */ 99 protected abstract boolean hasSharedThreadPool(); 100 101 /** 102 * @return <tt>true</tt> unless stopped or finished. 103 */ 104 protected abstract boolean canSchedule(); 105 }