This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Using Thread.run() execute two threads simultaneously in an Enhanced loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Using Thread.run() execute two threads simultaneously in an Enhanced loop" Watch "Using Thread.run() execute two threads simultaneously in an Enhanced loop" New topic
Author

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

rathishkumar kumar
Greenhorn

Joined: May 07, 2013
Posts: 2




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?


Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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

Joined: May 07, 2013
Posts: 2
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

Joined: May 30, 2012
Posts: 679
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.*.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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.
 
GeeCON Prague 2014
 
subject: Using Thread.run() execute two threads simultaneously in an Enhanced loop