• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

How to run threads in parallel?  RSS feed

 
Bartender
Posts: 1661
17
  • Mark post as helpful
  • send pies
  • 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: 10104
212
  • Mark post as helpful
  • send pies
  • 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: 1661
17
  • Mark post as helpful
  • send pies
  • 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: 10104
212
  • Mark post as helpful
  • send pies
  • 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: 1661
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    All fixed. Thanks!!!

    -- mike
     
    Marshal
    Posts: 64089
    215
    • Mark post as helpful
    • send pies
    • 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: 10104
    212
    • Mark post as helpful
    • send pies
    • 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.
     
    Beware the other head of science - it bites! Nibble on this message:
    Create Edit Print & Convert PDF Using Free API with Java
    https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!