Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Andrey Adamovich - Groovy 2 Cookbook - 2013.pdf
Скачиваний:
44
Добавлен:
19.03.2016
Размер:
26.28 Mб
Скачать

Concurrent Programming in Groovy

There's more...

In this short recipe, we only tried out few of the many "parallel" methods. In the following recipes, we will see more examples of the Parallelizer in action. For a complete

list of parallel operations, refer to the Javadoc page of GParsUtil: http://gpars. org/1.0.0/javadoc/groovyx/gpars/GParsPoolUtil.html.

See also

ff Downloading files concurrently

ff http://gpars.codehaus.org/

ff http://www.gpars.org/guide/

ff http://gpars.org/1.0.0/javadoc/groovyx/gpars/GParsPoolUtil.html

ff http://nlp.stanford.edu/nlp

ff http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/ process/PTBTokenizer.html

Downloading files concurrently

This recipe is about downloading files concurrently from the network. As for most recipes in this chapter, we will use the GPars framework to leverage the concurrent features required by the parallel downloading.

Getting ready

This recipe reuses the same build infrastructure created in the Processing collections concurrently recipe.

How to do it...

The download logic is completely encapsulated in a Groovy class.

1.Add a new FileDownloader class to the src/main/groovy/org/groovy/ cookbook folder:

package org.groovy.cookbook

import static groovyx.gpars.GParsPool.*

import static com.google.common.collect.Lists.* class FileDownloader {

340

www.it-ebooks.info

Chapter 10

static final int POOL_SIZE = 25 static pool

FileDownloader() {

pool = createPool(POOL_SIZE)

}

private void downloadFile(String remoteUrl, String localUrl) {

new File("$localUrl").withOutputStream { out -> new URL(remoteUrl).withInputStream { from -> out << from

}

}

}

private void parallelDownload(Map fromTo) { withExistingPool(pool) {

fromTo.eachParallel { from, to -> downloadFile(from, to)

}

}

}

void download(Map fromTo, int maxConcurrent) { if (maxConcurrent > 0) {

use(MapPartition) {

List maps = fromTo.partition(maxConcurrent) maps.each { downloadMap ->

parallelDownload(downloadMap)

}

}

}else { parallelDownload(fromTo)

}

}

}

class MapPartition {

static List partition(Map delegate, int size) {

def rslt = delegate.inject( [ [:] ] ) { ret, elem -> (ret.last() << elem).size() >= size ?

ret << [:] : ret

}

rslt.last() ? rslt : rslt[0..-2]

}

}

341

www.it-ebooks.info

Concurrent Programming in Groovy

2.Let's write a unit test, to test our newly created class. Don't forget to place the test in the src/main/groovy/org/groovy/cookbook folder:

package org.groovy.cookbook import org.junit.*

class FileDownloaderTest2 {

static final DOWNLOAD_BASE_DIR = '/tmp' static final TEST_SERVICE =

'https://androidnetworktester.googlecode.com' static final TEST_URL =

"${TEST_SERVICE}/files/1mb.txt?cache="

def downloader = new FileDownloader() Map files

@Before

void before() { files = [:] (1..5).each {

files.put( "${TEST_URL}1.${it}",

"${DOWNLOAD_BASE_DIR}/${it}MyFile.txt"

)

}

}

@Test

void testSerialDownload() {

long start = System.currentTimeMillis() files.each{ k,v ->

new File(v) << k.toURL().text

}

long timeSpent = System.currentTimeMillis() - start println "TIME NOPAR: ${timeSpent}"

}\

@Test

void testParallelDownload() {

long start = System.currentTimeMillis() downloader.download(files, 0)

long timeSpent = System.currentTimeMillis() - start

342

www.it-ebooks.info

Chapter 10

println "TIMEPAR: ${timeSpent}"

}

@Test

void testParallelDownloadWithMaxConcurrent() { long start = System.currentTimeMillis() downloader.download(files, 3)

long timeSpent = System.currentTimeMillis() - start println "TIMEPAR MAX 3: ${timeSpent}"

}

}

3.As usual, execute the test by issuing the following command in your shell: groovy -i clean test

4.The results are highly dependent on your network latency, but you should see an output as follows:

TIME NOPAR: 635 TIMEPAR: 391 TIMEPAR MAX 3: 586

How it works...

The FileDownloader class uses the Parallel Arrays implementation offered by GPars.

This implementation provides parallel variants of the common Groovy iteration methods such as each, collect, and findAll. Every time you come across a collection that is slow to process, consider using parallel collection methods. Although enabling collections for parallel processing imposes a certain overhead (mostly because of the cost of initializing a thread pool), it frequently outweighs the ineffectiveness of processing a collection in a sequential fashion. GPars gives you two options here:

ff GParsPool, which uses the "fork/join" algorithm, using a "fork/join" based thread pool;

ff GParsExecutorsPool, which uses the Java 5 executors.

In the majority of cases, the first option is more efficient, but it is always worth trying both thread pools, to verify which one performs better for a specific case.

The FileDownloader class resorts to GParsPool, which gets initialized in the class constructor. The pool creation operation is an expensive one and adds the higher overhead on the parallel framework.

343

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]