DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Concurrency in JSR-166y Meets Groovy: Process Collections in Parallel

Concurrency in JSR-166y Meets Groovy: Process Collections in Parallel

Vaclav Pech user avatar by
Vaclav Pech
·
Sep. 26, 08 · News
Like (0)
Save
Tweet
Share
10.63K Views

Join the DZone community and get the full member experience.

Join For Free

How about parallel processing of elements stored in one of the java.util.* collections? Could we make the Groovy each(), collect() and such methods leverage multiple threads to speed them up? In fact, it is very easy.

I wrote briefly about the new concurrency enhancements planned for Java 7 under JSR-166y earlier. I'm still happily experimenting with the library and here's how you could seamlessly employ ParallelArrays to speed up your Groovy collections. Thanks to Groovy meta-programming capabilities we will replace some of the original GDK methods on collections with parallel implementations.

It'll be a short journey, the target is only two steps away.

1. Create the JSR-166y thread pool and a helper method that will create Parallel Arrays for us

//Create a pool with size close to the number of processor's cores
final ForkJoinPool pool = new ForkJoinPool(2)

private ParallelArray createParallelArray(pool, collection) {
return ParallelArray.createFromCopy(
collection.toArray(new Object[collection.size()]), pool)
}

2. Enhance the required collection classes with parallel methods using meta-programing

We can either introduce new methods, something like eachAsync(), collectAsync(), etc., or replace directly the original GDK methods like each(), collect() and such. I'll go the second way in the example and replace the GDK methods on the ArrayList class, although replacing them on individual instances of the class instead would probably be more practical in reality.

//Enhance ArrayLists with a new method to process collect in parallel
ArrayList.metaClass.collect = {Closure cl ->
createParallelArray(pool, delegate).
withMapping({cl(it)} as Op).all().asList()
}

//Enhance ArrayLists with a new method to find matching objects in parallel
ArrayList.metaClass.findAll = {Closure cl ->
createParallelArray(pool, delegate).
withFilter({cl(it)} as Predicate).all().asList()
}

The magic happens now 

Although nothing needs to change in our code, the collections now use parallelism under the covers.

def sites=[
'http://www.jroller.com',
"http://www.infoq.com",
"http://java.dzone.com"]

def groovySites = sites.findAll {new URL(it).text.toLowerCase().contains('groovy')}
println "These sites talk about Groovy today: ${groovySites}"

Or if you need to process complex images in some way:

//Use the new parallel functionality
List images=loadImages()
List reformatedImages=images.collect {processImage(it)}

Did you count how many times we had to use the Thread class and the synchronized block in the multi-threaded code we've just written?

From http://jroller.com/vaclav/

Groovy (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Solving the Kubernetes Security Puzzle
  • Top 10 Best Practices for Web Application Testing
  • How To Build a Spring Boot GraalVM Image
  • Apache Kafka Is NOT Real Real-Time Data Streaming!

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: