Understanding multithread

82 Views Asked by At

I have questions regarding the multithread processign using Ant fork approach

Below are the Jar files and associated java classes and methods

Test1.jar has --Test1.java has --MethodA()

Test2.jar has --Test2.java has --MethodB()

Test3.jar has --Test3.java has --MethodC()

runtime.classpath will have: Test1.jar,Test2.jar and Test3.jar

testsuite.list: Test1.tsv,Test2.tsv,Test3.tsv

Test1.tsv --> calls MethodC(), MethodB(), MethodA() in sequence
Test2.tsv --> calls MethodA(), MethodB(), MethodA() in sequence
Test3.tsv --> calls MethodB(), MethodA(), MethodA() in sequence

Ant sciprt:

<for list="${testsuite.list}" param="testsuite" parallel="true" threadCount="3">
 <sequential>
    <local name="cmdLine.args" />
    <property name="cmdLine.args" value="--variable ENV:${env.file} --listener TListener ${tag.option} @{testsuite}"/>
    <java classname="org.TestWork"  classpathref="runtime.classpath" fork="true" maxmemory="1024M">
        <arg line="${cmdLine.args}"/>
    </java>
 </sequential>
</for>
<java classname="org.TestWork"  classpathref="runtime.classpath" fork="true" maxmemory="1024M">
  <arg value="--output"/>
  <arg value="output.xml"/>
</java> 

When Ant scripts triggered

Thread 1 pickup Test1.tsv Thread 2 pickup Test2.tsv Thread 3 pickup Test3.tsv

Now my question is when 3 threads executing in parallel, 3 instances of each Test1.java, Test2.java and Test3.java are created Or there will be only one instance of Test1.java, Test2.java and Test3.java?

If only one instance of Test1.java, Test2.java and Test3.java created while Thread1 exeucting MethodA() , Thread2 has to wait until Thread1 completes MethodA() execution?

Can some help me to understand this. Also, is there a way I can know how many threads executing given method at a time , is there any locking happening or how many java instances created?

Other point to note is my MethodA(),MethodB(),MethodC() has implementations that have wait time and retry mechanism to look for web elements. So I am not sure when multiple threads executing what’s happening .. Sometime I am seeing lot of delay to complete method execution so wondering if there is any locking happening.

1

There are 1 best solutions below

5
Supun Wijerathne On

As per your description, I there are some shared global resources that are accessed by threads. So threads should be sharing common resources not separated ones.

If only one instance of Test1.java, Test2.java and Test3.java created while Thread1 exeucting MethodA() , Thread2 has to wait until Thread1 completes MethodA() execution?

Yes, Correct. But no one can guarantees the order.

Also, is there a way I can know how many threads executing given method at a time?

Yes, There are few ways to do,

  • You can use a lock + another protected global variable that will be incremented at the start of the method & decremented at the end of the method. (Old way - more explicit)
  • A semaphore - you can use getQueueLength() for this purpose. (Easiest way - less explicit implementation)
  • A synchronized block.

I prefer the second one (a semaphore) since it is the most powerful & easiest.

is there any locking happening or how many java instances created?

There will not be any default locking happening unless you implemented or used synchronized.