• 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

Using Thread.run() execute two threads simultaneously in an Enhanced loop

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




public abstract class Multithread implements Runnable{
static Thread t1 = new Thread(){
public synchronized void run(){
try {
for(;;){
System.out.println("java");
t1.sleep(300);
}
} catch (Exception e) {
System.out.println("Exception"+e);
}
}
};
static Thread t2 = new Thread(){
public synchronized void run(){
try{
for(;;){
System.out.println("world");
t2.sleep(300);
}
}catch(Exception e){
System.out.println("Exception"+e);
}
}
};
public static void main(String[] args) {
try{
t1.start();
t2.start();
}catch(Exception e){
System.out.println("Exception "+e);
}
}
@Override
public void run() {
System.out.println("running");
}
}

Excepted OUTPUT:
java world java world java world . . . .

observed I tried using sleep() for the threads,they are getting overlapped at some point of time like this- java java world java world world java ..

Expected
i need these two threads to run in parallel and it should not get overlapped,either of the thread can be started. Any idea?


 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's how threads work. You can't predict the order they will execute in and a delay of only 300ms will probably have little effect on the order.
 
rathishkumar kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:That's how threads work. You can't predict the order they will execute in and a delay of only 300ms will probably have little effect on the order.


Hi Stuart A. Burkett,
The order of the execution is not a matter any thread can be executed first, in the enhanced loop each thread should execute once (i.e., if t1 starts execution means t1,t2,t1,t2....). this is the requirement.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rathishkumar kumar wrote:

Stuart A. Burkett wrote:That's how threads work. You can't predict the order they will execute in and a delay of only 300ms will probably have little effect on the order.


Hi Stuart A. Burkett,
The order of the execution is not a matter any thread can be executed first, in the enhanced loop each thread should execute once (i.e., if t1 starts execution means t1,t2,t1,t2....). this is the requirement.


Okay maybe I wasn't clear. By order of execution I did not mean the order in which the threads start. I was referring to the order in which time is alloocated to each thread during which they can execute instructions.
If you were hoping each thraed would take it in turns to print out a word, then you are going to be disappointed. It might appear to work for a while if you put a longer delay in, but if you leave your program running long enough, you will probably get out of order words eventually.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rathishkumar kumar wrote:

Stuart A. Burkett wrote:That's how threads work. You can't predict the order they will execute in and a delay of only 300ms will probably have little effect on the order.


Hi Stuart A. Burkett,
The order of the execution is not a matter any thread can be executed first, in the enhanced loop each thread should execute once (i.e., if t1 starts execution means t1,t2,t1,t2....). this is the requirement.



That's not how threads work. They would be comletely useless if they had to take turns in lockstep like that.

Even with those sleep() calls in there, it's completely possible for t1 to exeute to completion before t2 does anything, and it's also completely possible for t2 to execute to completion before t1 does anything. And other combinations are possible as well. And if you run your program 100 times you might see the output order you want once, and various other combinations the other 99 times. We can't predict it. And that's a good thing.

If you want lockstep alternation, you'll have to introduce inter-thread communication such as with wiat/notifyAll or using the constructs in java.util.concurrent.*.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:Okay maybe I wasn't clear. By order of execution I did not mean the order in which the threads start. I was referring to the order in which time is allocated to each thread during which they can execute instructions.
If you were hoping each thraed would take it in turns to print out a word, then you are going to be disappointed. It might appear to work for a while if you put a longer delay in, but if you leave your program running long enough, you will probably get out of order words eventually.


Well, again, that is how threads work

If you are aware of various states of thread, a thread is picked from 'runnable' state by thread-scheduler, and then is executed (and its state is changed to 'running'). (Note: Please do not confuse between 'runnable' state and 'Runnable' interface)

When you invoke start method on a thread, you simply push that thread to runnable state.

Now, due to the way thread-scheduler works, there's no guarantee that just because thread A is marked as runnable before thread B, thread A will enter running state before thread B. It can legally happen that when you say

and thread scheduler will choose to execute ThreadB first.

Also, it is thread-scheduler's task to do thread-context-switching. Its not always necessary that both thread will run in parallel (e.g. if execution of a thread is going to take very less time, then scheduler may decide to run another thread after this thread is completed).

I hope this answers your question.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:I hope this answers your question.


Was that aimed at me ? You quoted my post in it, but my post was not a question.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic