• Post Reply Bookmark Topic Watch Topic
  • New Topic

do these two threads start simultaneously or sequencially ?

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyThread implements Runnable {

...
public void run() {
// do something;
}
}

class StartTwoThread {
public static void main(String[] args) {
MyThread t1 = new MyThread(..);
MyThread t2 = new MyThread(..);
t1.start();
t2.start();
}
}

Question -- I want to simulate t1 and t2 to simultaneously do something on a single object, but I just don't know how to exactly let them start at the same time. For the above code, will t1 and t2 start at the same time ? or will t1 start "little" bit earlier than t2 does ?
 
Vijay Jagannathan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question -- I want to simulate t1 and t2 to simultaneously do something on a single object, but I just don't know how to exactly let them start at the same time. For the above code, will t1 and t2 start at the same time ? or will t1 start "little" bit earlier than t2 does ?


We cannot be really sure whether t1 or t2 will start first. It depends on the VM and other various factors.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are not guaranteed to start sequentially. If you want to get threads to access the same piece of code generally we have to put in a loop and have it do the thing LOTS of times (like 1,000,000+) and put in some tests to check for the violation condition we are after. But in general you learn the design principles of multithreaded coding not so much by trial and error. But there is always error.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They'll start as close to "at the same time" as you can get. As mentioned above there is no guarantee they'll start in any particular order, or how close together they'll start, but I don't think you can do any better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!