Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter to configure number of threads used in device operations #304

Merged
merged 1 commit into from Mar 19, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -54,6 +54,8 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
Expand All @@ -70,6 +72,7 @@
* @author Manfred Moser <manfred@simpligility.com>
* @author William Ferguson <william.ferguson@xandar.com.au>
* @author Malachi de AElfweald malachid@gmail.com
* @author Roy Clarkson <rclarkson@gopivotal.com>
*/
public abstract class AbstractAndroidMojo extends AbstractMojo
{
Expand Down Expand Up @@ -272,6 +275,16 @@ public abstract class AbstractAndroidMojo extends AbstractMojo
* @parameter expression="${android.devices}"
*/
protected String[] devices;

/**
* <p>Specifies the number of threads to use for deploying and testing on attached devices.
*
* <p>This parameter can also be configured from command-line with
* parameter <code>-Dandroid.deviceThreads=2</code>.</p>
*
* @parameter expression="${android.deviceThreads}"
*/
protected int deviceThreads;

/**
* A selection of configurations to be included in the APK as a comma separated list. This will limit the
Expand Down Expand Up @@ -737,6 +750,17 @@ protected void doWithDevices( final DeviceCallback deviceCallback )
throw new MojoExecutionException( "No online devices attached." );
}

int threadCount = getDeviceThreads();
if ( getDeviceThreads() == 0 )
{
getLog().info( "android.devicesThreads parameter not set, using a thread for each attached device" );
threadCount = numberOfDevices;
}
else
{
getLog().info( "android.devicesThreads parameter set to " + getDeviceThreads() );
}

boolean shouldRunOnAllDevices = getDevices().size() == 0;
if ( shouldRunOnAllDevices )
{
Expand All @@ -748,6 +772,7 @@ protected void doWithDevices( final DeviceCallback deviceCallback )
}

ArrayList<DoThread> doThreads = new ArrayList<DoThread>();
ExecutorService executor = Executors.newFixedThreadPool( threadCount );
for ( final IDevice idevice : devices )
{
if ( shouldRunOnAllDevices )
Expand All @@ -764,11 +789,14 @@ public void runDo() throws MojoFailureException, MojoExecutionException
}
};
doThreads.add( deviceDoThread );
deviceDoThread.start();
executor.execute( deviceDoThread );
}
}

joinAllThreads( doThreads );
executor.shutdown();
while ( ! executor.isTerminated() )
{
// waiting for threads finish
}
throwAnyDoThreadErrors( doThreads );

if ( ! shouldRunOnAllDevices && doThreads.isEmpty() )
Expand All @@ -777,21 +805,6 @@ public void runDo() throws MojoFailureException, MojoExecutionException
}
}

private void joinAllThreads( ArrayList<DoThread> doThreads )
{
for ( Thread deviceDoThread : doThreads )
{
try
{
deviceDoThread.join();
}
catch ( InterruptedException e )
{
new MojoExecutionException( "Thread#join error for device: " + getDevices().toString() );
}
}
}

private void throwAnyDoThreadErrors( ArrayList<DoThread> doThreads ) throws MojoExecutionException,
MojoFailureException
{
Expand Down Expand Up @@ -1311,6 +1324,11 @@ private Set<String> getDevices()

return list;
}

private int getDeviceThreads()
{
return deviceThreads;
}

private abstract class DoThread extends Thread
{
Expand Down