View Javadoc

1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package net.sourceforge.basher.internal.tasks;
16  
17  import net.sourceforge.basher.BasherContext;
18  import net.sourceforge.basher.ContextManager;
19  import net.sourceforge.basher.Scheduler;
20  import org.apache.commons.logging.Log;
21  
22  /**
23   * @author Johan Lindquist
24   * @version 1.0
25   */
26  public class ThreadIncrementTask implements Runnable
27  {
28      private Log _log;
29      private Scheduler _scheduler;
30      private ContextManager _contextManager;
31      private boolean _skipIncrementing = false;
32  
33      public void setContextManager( final ContextManager contextManager )
34      {
35          _contextManager = contextManager;
36      }
37  
38      public void setLog( final Log log )
39      {
40          _log = log;
41      }
42  
43      public void setScheduler(final Scheduler scheduler)
44      {
45          _scheduler = scheduler;
46      }
47  
48      public void run()
49      {
50          try
51          {
52              final BasherContext basherContext = _contextManager.getActiveBasherContext();
53              int threadIncrementCount = basherContext.getThreadIncrementCount();
54  
55              if (threadIncrementCount == 0)
56              {
57                  _log.warn("Thread increment of '0' thread(s)");
58                  return;
59              }
60  
61              if (_scheduler.getNumberOfActiveThreads() == basherContext.getMaxNumberThreads() && !_skipIncrementing)
62              {
63                  _log.info( "Maximum threads reached, not incrementing further" );
64                  _skipIncrementing = true;
65                  return;
66              }
67  
68              if (_skipIncrementing)
69              {
70                  // Simply return
71                  return;
72              }
73  
74  
75              // Reset this to false in case thread count has dropped
76              _skipIncrementing = false;
77  
78              _log.info("Incrementing thread count by '" + threadIncrementCount + "' thread(s)");
79              _scheduler.addThreads(threadIncrementCount);
80              _log.info("Number of threads incremented by '" + threadIncrementCount + "' thread(s)");
81  
82          }
83          catch (Throwable e)
84          {
85              _log.error(e.getMessage(), e);
86          }
87  
88      }
89  }