I am attaching a threads tag library to expand on the current ThreadTag.
This library adds:

MutexTag
SynchronizeTag (synchronize on a mutex)
WaitTag/NotifyTag (wait/notify on passed in mutex)
InterruptTag (thread interrupt)
JoinTag (thread join)
SleepTag
GroupTag (thread group)
WaitForTag (wait for a specific thread to finish. you can also wait for a
specific status FAILURE, SUCCESS, AVOIDED)

Example: The threads should increment together. Uses wait/notify

        <set var="result" value=""/>
        <threads:mutex var="mutex"/>

        <threads:thread var="thread1">
            <threads:synchronize mutex="${mutex}">
                <set var="cnt1" value="0"/>
                <while test="${cnt1 != 5}">
                    <set var="result" value="${result}${cnt1}"/>
                    <set var="cnt1" value="${cnt1 + 1}"/>

                    <!-- let the other thread go -->
                    <threads:notify mutex="${mutex}"/>
                    <threads:wait mutex="${mutex}"/>

                </while>

                <!-- one last notify -->
                <threads:notify mutex="${mutex}"/>

            </threads:synchronize>
        </threads:thread>

        <threads:thread var="thread2">

            <!-- let the other thread start -->
            <threads:sleep for="100"/>

            <threads:synchronize mutex="${mutex}">
                <set var="cnt2" value="0"/>
                <while test="${cnt2 != 5}">
                    <set var="result" value="${result}${cnt2}"/>
                    <set var="cnt2" value="${cnt2 + 1}"/>

                    <!-- let the other thread go -->
                    <threads:notify mutex="${mutex}"/>
                    <threads:wait mutex="${mutex}"/>

                </while>

                <!-- one last notify -->
                <threads:notify mutex="${mutex}"/>

            </threads:synchronize>
        </threads:thread>


Example: uses the waitfor tag. thread 1 should go 1st, then 2.


        <set var="result" value=""/>

        <threads:thread var="thread1">
            <set var="cnt1" value="0"/>
            <while test="${cnt1 != 5}">
                <set var="result" value="${result}${cnt1}"/>
                <set var="cnt1" value="${cnt1 + 1}"/>

                <!-- force an interrupt, shouldn't matter -->
                <threads:interrupt/>

            </while>
        </threads:thread>

        <threads:thread var="thread2">

            <!-- wait for thread1 -->
            <threads:waitfor thread="${thread1}"/>

            <set var="cnt2" value="5"/>
            <while test="${cnt2 != 10}">
                <set var="result" value="${result}${cnt2}"/>
                <set var="cnt2" value="${cnt2 + 1}"/>
            </while>
        </threads:thread>


Example: use waitFor to show how to wait on a specific status


        <set var="result" value=""/>

        <threads:thread var="thread1">
            THROW AN EXCEPTION
            <bsh:script>
                throw new Exception("This exception SHOULD happen");
            </bsh:script>
        </threads:thread>

        <threads:thread var="thread2">

            <!-- this will fail since thread 1 is sleeping -->
            <threads:waitfor thread="${thread1}" status="SUCCESS"/>

            <set var="cnt2" value="5"/>
            <while test="${cnt2 != 10}">
                <set var="result" value="${result}${cnt2}"/>
                <set var="cnt2" value="${cnt2 + 1}"/>
            </while>
        </threads:thread>

        <threads:thread var="thread3">

            <!-- this will fail since thread 1 is sleeping -->
            <threads:waitfor thread="${thread1}" status="FAILURE"/>

            <set var="cnt2" value="10"/>
            <while test="${cnt2 != 15}">
                <set var="result" value="${result}${cnt2}"/>
                <set var="cnt2" value="${cnt2 + 1}"/>
            </while>
        </threads:thread>


See the test suite (suite.jelly) and the other .jelly files in the test
directory for more examples.

-jason horman
 [EMAIL PROTECTED]
 [EMAIL PROTECTED] 
This email message and any attachments are for the sole use of the intended
recipient(s) and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited. If you
are not the intended recipient or his/her representative, please contact the
sender by reply email and destroy all copies of the original message.
  

Attachment: threads.tar.gz
Description: Binary data

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>

Reply via email to