Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8010445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:44:12+00:00 2026-06-04T18:44:12+00:00

I have a question to Groovy threads. My task is to translate each file

  • 0

I have a question to Groovy threads.

My task is to translate each file in a given directory in a certain manner
and place the resulting output in a file in an other directory.

I wrote the following code, which works:

static def translateDir(fromDir, targetDir) {
    def allFiles = new File(fromDir).listFiles()
    def numFiles = allFiles.length

    for (i in 0..(numFiles - 1))
        translate(allFiles[i].getAbsolutePath(), targetDir)
}

Now, I tried to parallelize this code like this:

static def translateDir(fromDir, targetDir) {
    def allFiles = new File(fromDir).listFiles()
    def numFiles = allFiles.length
    def numCores = Runtime.getRuntime().availableProcessors()

    for (i in 0..(numCores - 1)) {
        println("Thread " + i + "starting")
        Thread.start {
            for (def j = i; j < numFiles; j += numCores) {
                println("j = " + j) 
                translate(allFiles[j].getAbsolutePath(), targetDir)
            }
        }
    }
}

which does not work and provides the output:

Thread 0 starting
Thread 1 starting
Thread 2 starting
Thread 3 starting

nunCores is 4, and numFiles is 3 in my test case.
What is going on here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:44:13+00:00Added an answer on June 4, 2026 at 6:44 pm

    ok, 2 things:

    1. Inside of the thread’s implicit run() method, you are referencing the ‘i’ variable. I’d have to step it through a debugger to see exactly what happens, but technically, you shouldn’t even have access to ‘i’ there, because it’s not final. So, I’d suggest creating a Runnable object to which you pass ‘i’ in the constructor. Then start the runnable on the thread.

    2. Groovy has great concurrency support – no reason to roll your own like this. Take a look at GPars

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

may be too simple groovy question but....please help i have a list like this:
i'm learning groovy and I have a question concerning its metaprogramming facilities. From what
This is a follow up to the question asked here: Groovy parsing text file
There are already several threads where this question is discussed, and I have already
Similar to this question I have a small groovy test script that basically uses
I have a very basic question: Why did Twitter choose Scala rather than Groovy
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have question concerning a function I created. I would like to show the
I have question related to what is done in SMS application is if i'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.