1
2
3
4
5
6
7
8
9
10
11
12
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
24
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
71 return;
72 }
73
74
75
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 }