• 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

do these two threads start simultaneously or sequencially ?

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic