Cornerstone Components - Threads

Example

Source Description
ThreadConsumer.java A demonstration component implementation demonstrates the resolution of a ThreadManager and subsequent access to a ThreadPoll against which a Runnable object is launched. Termination of the container before this time will trigger disposal of the component causing the interruption of the subsidiary thread.
Counter.java The runnable object that simply initiates a countdown and will normally complete and terminate within 10 seconds. Used by the ThreadConsumer component to establish a threaded activity.
block.xml Merlin deployment directive.
<container name="thread-tutorial">

     <categories>
       <category name="classloader/scanner" priority="ERROR"/>
     </categories>

     <classloader>
       <classpath>
         <repository>
           <resource id="avalon-framework:avalon-framework-impl" version="4.1.5"/>
           <resource id="commons-collections:commons-collections" version="2.1"/>
           <resource id="excalibur-pool:excalibur-pool" version="1.2"/>
           <resource id="excalibur-thread:excalibur-thread" version="1.1.1"/>
           <resource id="excalibur-event:excalibur-event" version="1.0.3"/>
           <resource id="cornerstone-threads:cornerstone-threads-api" version="1.0"/>
           <resource id="cornerstone-threads:cornerstone-threads-impl" version="1.0"/>
         </repository>
       </classpath>
     </classloader>

     <component name="threads" 
        class="org.apache.avalon.cornerstone.blocks.threads.DefaultThreadManager">
       <configuration>
         <thread-group>
           <name>default</name>
           <priority>5</priority> 
           <is-daemon>false</is-daemon>
           <max-threads>100</max-threads>
           <min-threads>20</min-threads>
           <min-spare-threads>20</min-spare-threads>
         </thread-group>
       </configuration>
     </component>

     <component name="test" 
        class="org.apache.avalon.cornerstone.threads.tutorial.ThreadConsumer" />

</container>

Runtime

To execute this demonstration please make sure that you copy the cornerstone-threads-* jar files from your Maven repository to you Merlin repository.

$ cd avalon-cornerstone\threads\tutorial
$ merlin target\cornerstone-threads-tutorial-1.0.jar

[INFO   ] (thread-tutorial.test): aquiring cornerstone threads service
[INFO   ] (thread-tutorial.test): initialization
[INFO   ] (thread-tutorial.test.counter): count: 10
[INFO   ] (thread-tutorial.test.counter): count: 9
[INFO   ] (thread-tutorial.test.counter): count: 8
[INFO   ] (thread-tutorial.test.counter): count: 7

Interrupting the container (using ^C) will trigger component disposal which in turn will trigger early termination of the thread established using the cornerstone thread manager.

[INFO   ] (thread-tutorial.test): disposal
[INFO   ] (thread-tutorial.test): disposal invoked while child thread active
[INFO   ] (thread-tutorial.test.counter): I've been interrupted.
[INFO   ] (thread-tutorial.test.counter): Time to die.
[INFO   ] (thread-tutorial.test): disposal complete