• 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

Starting two threads at the same time.

  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
Is there a way of starting two threads at exactly the same time in Java?
If t1 and t2 are our two threads, can we start both the threads at the same time, say tx?
This was an interview question I was asked. My answer was "No.", but then, the interviewer asked if I know of concurrent API.
I didn't know of concurrent API. Is there a way of doing it in concurrent API?
Thanks in advance.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use java.util.concurrent.CountDownLatch to do that.
However, I doubt it will be exactly the same time.
It depends on what you mean by exactly the same time.
 
Marshal
Posts: 79254
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would be able to do that on a two‑core machine, but not on a one‑core machine. On a one‑core machine, threads (and all other processing) have to take turns.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even on a multi-core machine, there is no way to guarantee that two threads run at exactly the same time - whatever is meant by that.

The interviewer was probably using sloppy language, and wanted to know if you knew how to use concurrency classes such as CountDownLatch (that Paweł already mentioned) or Semaphore.
 
Sreevatsa Turuvekere Laxmi Narayana
Greenhorn
Posts: 10
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:The interviewer may probably wanted to know if you knew how to use concurrency classes such as CountDownLatch (that Paweł already mentioned) or Semaphore.


I agree with you. Actually, after him asking about concurrent API, it took me a while to gather all that I knew about threads and go about answering the question.
Going through CountDownLatch and CyclicBarrier gave me some idea.
Thank you Jesper, Campbell and Pawell.
While I'm at this question, I want to ask one more related question.
Suppose I have three threads starting from the main thread (3 + 1) and the program is to be run in a quad-core processor system.
If thread-1 is running in core-1, then no other thread (remaining 2 threads + 1 main thread) should run in that processor's core.
In the above scenario, is there a way of specifying one thread to be executed in one core of the processor and another thread in a different core?
If yes, can we specify it while starting the program using some runtime VM arguments?
And, can we code it in program so that the thread itself chooses one of the available cores to run and complets all its execution in a single core of the processor?

If the above "single-thread::single-core" scenario is possible, can't we use CountDownLatch and CyclicBarrier and start and run all the theads at the same time considering there is no other contention for each core? Or, is the question not relevant at all?
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic