• Post Reply Bookmark Topic Watch Topic
  • New Topic

Starting two threads at the same time.  RSS feed

 
Sreevatsa Turuvekere Laxmi Narayana
Greenhorn
Posts: 10
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!