• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to run threads in parallel?

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created four threads.

When the code runs, I see:

Thread-0 is running.
Thread-1 is running.
Thread-2 is running.
Thread-3 is running.

Yet, when the code executes, all of Thread-0 runs first, then Thread-1 and so on.

Since I have a multi-core machine, I expected to see the thread numbers jumbled up to show that multiple threads were running at the same time, but that doesn't appear to be happening.

I am not using ".join()".

Just creating four threads and starting them, like this:

(Hard-coded loop is only for testing)



I've done some searching on this but it's still not clear.

Is creating threads enough to have the system execute them in parallel or do I really need to use fork-join framework?

Thanks,

-- mike
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be helpful if you showed us what the GenerateUUID task does. If literally all it does is generate a UUID, the threads probably finish before the next thread can do anything.

I don't think the call to Thread.sleep() does what you think it does. It's a static method that makes the current thread sleep, not the thread that you call it on. Indeed, calling it on an object reference is a probable mistake.

Threads will run in parallel without any extra effort on your part, but it's not a good idea to start your own threads. Use an ExecutorService instead.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great info!

I changed my code to use the Executor Service, like this:



This works great.

I ended up creating a separate file per thread as I was having difficulty synchronizing the writes to a single file using a FileWriter. Most of the file was OK, but there would be random writes (non synchronized in some way) here and there.

-- mike

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some mistakes in that code.

  • You're using the wrong name for your 5th task, it's called "UUID4".
  • You're only awaiting four of the five latches.
  • Shut down executor services in a finally-clause:

  •  
    Mike London
    Bartender
    Posts: 1971
    17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    All fixed. Thanks!!!

    -- mike
     
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:. . .

  • You're only awaiting four of the five latches.
  • . . .

    Is there any risk that cd1.await(); (line 25) will throw an exception and stop the other Latches from await()ing, or is that notion purely theoretical?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15510
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Purely theoretical. The current thread isn't interrupted anywhere. I prefer to enforce that assertion with an AssertionException, rather than printing a message.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic