View Javadoc
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 java.util.Queue;
23  import java.util.concurrent.ConcurrentLinkedQueue;
24  
25  /**
26   * The sequentially executing strategy in private package.
27   *
28   * @author Tibor Digana (tibor17)
29   * @see SchedulingStrategy
30   * @since 2.16
31   */
32  final class InvokerStrategy
33      extends SchedulingStrategy
34  {
35  
36      private final Queue<Thread> activeThreads = new ConcurrentLinkedQueue<Thread>();
37  
38      @Override
39      public void schedule( Runnable task )
40      {
41          if ( canSchedule() )
42          {
43              final Thread currentThread = Thread.currentThread();
44              try
45              {
46                  activeThreads.add( currentThread );
47                  task.run();
48              }
49              finally
50              {
51                  activeThreads.remove( currentThread );
52              }
53          }
54      }
55  
56      @Override
57      protected boolean stop()
58      {
59          return disable();
60      }
61  
62      @Override
63      protected boolean stopNow()
64      {
65          final boolean stopped = disable();
66  
67          for ( Thread activeThread = activeThreads.poll(); activeThread != null; activeThread = activeThreads.poll() )
68          {
69              activeThread.interrupt();
70          }
71          return stopped;
72      }
73  
74      @Override
75      public boolean hasSharedThreadPool()
76      {
77          return false;
78      }
79  
80      @Override
81      public boolean finished()
82          throws InterruptedException
83      {
84          return disable();
85      }
86  
87      public boolean destroy()
88      {
89          return stop();
90      }
91  }