Streaming Math in Parallel on Apache Ant

I am doing several sub-tasks in parallel with Ant.

This is the simplified content of build.xml:

<target name="parallelOperations">
    <var name="port.number" value="9006"/>
    <for list="a,b,c,d,e" param="letter" parallel="true">
        <sequential>
            <echo>Letter @{letter}</echo>
            <math result="port.number" operand1="${port.number}" operation="+" operand2="1" datatype="int"/>
            <echo>${port.number}</echo>
        </sequential>
    </for>
</target>

      

This is the main result:

 [echo] Letter c
 [echo] Letter b
 [echo] Letter a
 [echo] Letter d
 [echo] Letter e
 [echo] 9007
 [echo] 9007
 [echo] 9007
 [echo] 9007
 [echo] 9007

      

What happens here for each list item is it prints the contents of the item and the result plus the operations. The problem is that the math operation is not stream saving, which means that each operation simultaneously processes the $ port.number variable, adds one value and then assigns a value.

Is there a way to make this thread safe using Ant? What I am trying to do is get a unique port number for each sub-task that runs in parallel. If there is any other way to do this, this might be a good solution.

+3


source to share


2 answers


This is from the documentationparallel

:

The main use case <parallel>

is concurrent execution of external programs such as an application server and JUnit or TestNG test suites. Anyone trying to run large Ant task sequences in parallel, such as javadoc and javac at the same time, implicitly takes on the task of identifying and fixing all the concurrency task errors they run .

Therefore, the timing of the operation is up to the user. There are two possible solutions:



The first solution is simpler and is usually designed for this kind of synchronization.

+3


source


I'm going to answer my own question by posting a specific solution, in case anyone needs it.

This is a working solution:

<target name="ParallelOperations">
<var name="port.number" value="9006"/>
<for list="a,b,c,d" param="letter" parallel="true">
    <sequential>
        <echo>Letter @{letter}</echo>
        <!-- Synchronize the port number -->
        <synchronized id="port.number.lock">
            <echo>@{letter} new port ${port.number}</echo>
            <var name="@{letter}.port.number" value="${port.number}" />
            <math result="port.number" operand1="${port.number}" operation="+" operand2="1" datatype="int"/>
        </synchronized>
        <echo>@{letter} ${@{letter}.port.number}</echo>
        <echo>@{letter} ${@{letter}.port.number}</echo>
        <echo>@{letter} ${@{letter}.port.number}</echo>
        <echo>@{letter} ${@{letter}.port.number}</echo>
        <echo>@{letter} ${@{letter}.port.number}</echo>
        <echo>@{letter} ${@{letter}.port.number}</echo>
        <echo>@{letter} ${@{letter}.port.number}</echo>
        <echo>@{letter} ${@{letter}.port.number}</echo>
        </sequential>
</for>

      

And this is the result:



ParallelOperations:
     [echo] Letter d
     [echo] Letter a
     [echo] Letter c
     [echo] Letter b
     [echo] c new port 9006
     [echo] c 9006
     [echo] b new port 9007
     [echo] c 9006
     [echo] c 9006
     [echo] b 9007
     [echo] c 9006
     [echo] a new port 9008
     [echo] b 9007
     [echo] c 9006
     [echo] b 9007
     [echo] c 9006
     [echo] a 9008
     [echo] d new port 9009
     [echo] c 9006
     [echo] b 9007
     [echo] a 9008
     [echo] d 9009
     [echo] b 9007
     [echo] a 9008
     [echo] d 9009
     [echo] c 9006
     [echo] a 9008
     [echo] b 9007
     [echo] d 9009
     [echo] a 9008
     [echo] b 9007
     [echo] d 9009
     [echo] a 9008
     [echo] b 9007
     [echo] d 9009
     [echo] a 9008
     [echo] d 9009
     [echo] a 9008
     [echo] d 9009
     [echo] d 9009

      

From @manouti's answer, I took a synchronized command from Stefan Fanke and I use each letter to create a specific variable to store the port number.

As you can see, each letter has its own unique port number.

+1


source







All Articles