This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread's run() sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Watch "Thread New topic
Author

Thread's run() sequence

Fawad Khan
Ranch Hand

Joined: Feb 13, 2001
Posts: 36
Hi,
Look at the following code.
class Threadtry implements Runnable{
public static void main(String args[]){
Threadtry TT = new Threadtry();
Thread t1 = new Thread(TT);
t1.start();
TT.myMethod();
}

public void run(){
try{
for (int i = 0; i < 10; i++){
System.out.println("The value of i is "+i);
Thread.sleep(1000);
System.out.println("After waking up.....");
}

}catch (InterruptedException e) {
System.out.println("in catch block.....");
}
} // end of run

void myMethod(){
System.out.println("In method........");
}
} // end of class
When the start() method calls run(), then why does myMethod() runs first, every time ? Please explain...


Seeking Certification
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
You are thinking of this in terms of procedrual practice. Like you call one method, it goes there, once completed it goes back to where it was called and goes from there.
That is not the case with threads. When you start a thread, you are saying I want to start a whole new process. In java, when you call start(), it passes that thread to the schedular and the thread schedular determines which threads run. In your example, the schedular has two threads to choose from, it can let main continue to run, or it can pass control to the theard you just created. On your particular machine, it let's main continue to run, so myMethod goes first. However, that is not the case on every machine. One of the key points to remember is that you can not determine which thread will run first. Java left that to the implementors of the JVM.
Bill
dropinboy
Greenhorn

Joined: Jan 16, 2001
Posts: 8
hi there
by calling the t1.start() it it doesn't call run() straight away, it just moves the thread into the ready-to-run state and is up to the thread schedular when t1 will run.
Fawad Khan
Ranch Hand

Joined: Feb 13, 2001
Posts: 36
Thanx Bill, you have given the complete explanation. My concept is very much clear now.
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Wait...Bill...wait..!!
in this context...
if the way that the thread scheduler determine which threads should run first is depend on the machine(the implementors of JVM), can you explain to me how JVM works to determine which kind of machine will run "THIS thread first", and which kind of machine will run "THAT thread first".
thank's in advance Bill...
stevie
Craig O'Brien
Greenhorn

Joined: Feb 28, 2001
Posts: 21
This is probably not testable, but... with a time slicing OS like Windows you may be able to get the first thread to run by calling setPriority(MAX_PRIORITY) before start(). Emphasize may.
With a "green thread" OS, like Solarus it probably won't make any difference.
The difference is preemptive multitasking (Win) vs cooperative multitasking (Solarus)
This is why
thread1.start();
thread2.start();
run()
for(; ;){
// repeating something
}
}
thread2 may never run.

sorry, I'm not use to the commands yet :0)
[This message has been edited by Craig O'Brien (edited February 28, 2001).]
[This message has been edited by Craig O'Brien (edited February 28, 2001).]
[This message has been edited by Craig O'Brien (edited February 28, 2001).]


Sun Certified Programmer for the Java 2 Platform<br />Senior Software Engineer<br />TechRx
Fawad Khan
Ranch Hand

Joined: Feb 13, 2001
Posts: 36
After these points another question comes to my mind that when we discuss the benefits of JAVA we say that it is platform independent. Means that it works same, on any platform u use it. And now we have come to the point that the behavior of the JVM is different on different machines. What do u say....?
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Yup Fawad...
ditto #@!*&^...!!!
confuse...confuse...and...confuse...!!!
Craig O'Brien
Greenhorn

Joined: Feb 28, 2001
Posts: 21
Don't let this upset you. Think of it as a good thing. The JVM attempts to work optimally within the bounds of the Operating System that it is contained by. If a computer has excellent hardware math operations then the JVM will attempt to use the native operations, if the OS has limitations with multiprocessing then the JVM will accept those limitations and do the best that it can.
Think of it this way. If the JVM is on a computer with only a 16 bit display, there is no way that it can override that limitation and display full color because of the limitation of the system. It will still work though. Operating systems impose different limitations as they are designed for different purposes. (some perhaps not as well designed as others) The JVM does a very good job considering this.
The JVM will always be at the mercy of its host. Or advantage with java is that we don't have to worry about datatypes being different sizes or bit order, or memory management. It is our job to write good code once, not 15 different ports of the same code. It is all the same to us. We just have to be aware of the few occasions where a problem can arise. This is one of those situations.
I would be interested to hear any other similar situations which can arise as the result of cross-platform compatibility issues. Better to know of potential problem areas first then to waste (too much) time and energy later.
All the best,
Craig
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Java attempts to be platform independent, but no language can be 100% platform independent. The key is to know which areas you have to take special caution with.
With threads, the purpose of threads is to be able to run mulitple processes at once, or at least give the illusion that you are running multiple processes. So if you really need one thread to run before the other thread, starting one first won't cut it. And if it did, then why even start the thread. Why not just call the method and then call the next method in the same thread. But if you do need to have more than one thread, and you do need one to act before the other, then you can use synchronized code to ensure one thread won't start until the other thread is finished. There are ways to do it, but not the way you listed.
And Craig, another area where platforms come into play is with I/O. When you create a File class you have to be careful how you instatiate the class. Java tries to resolve the differences between Windows, Unix, and Macs, but as a programmer you have to be aware that windows uses something like C:\ and unix uses root/.
Like I said, I don't think you can ever be 100% platform independent but you can minimize the impact by being aware of the places you need to take caution.
Bill
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Bill...
I know what you meant, but on your first replied to Fadwa, you didn't mention that "there is other ways to do it" (like using synchronization) to determine which thread should run first before the others.
maybe my concept about thread was not cleared yet.
But i think, reading your statement on your first replied who said :
(....."One of the key points to remember is that you can not determine which thread will run first. Java left that to the implementors of the JVM...."),
can make peoples (like me) end up with confusing, since that's the "key points to remember" you talking about.

but thank's Bill, your 2nd replied making it very clear now for me.
regards
stevie
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Sorry didn't mean to confuse you. Truth is you can't determine which thread will run first. Now if you use locks, you still can't say I want to lock this object first and this one second, but you can code it so that if the thread you don't want to start first, gets control you can call wait() and release the lock so your other thread can start. But you are still not saying, I want this one to start first.
Besides, it doesn't make sense to say that. By creating threads you are saying I want to have multiple things going on at the same time it isn't saying I want this to go first and then this. If that was the case you would only use one thread.
Just to elaborate on how you can use wait() to make sure one thread doesnt' get control before the other one does...
You could use a boolean b that you use as a switch. You set it to false to begin with and then in your thread, x, that you don't want to run before thread y, you would have while(!b) wait(); This will realese the lock so thread y can start. You are in effect saying make sure y has run before x runs, and since you are not sure which thread will run first, you have to code it this way and use locks so x cannot get control back.
Does that make sense?
Bill
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Thank's again Bill...
stevie
Fawad Khan
Ranch Hand

Joined: Feb 13, 2001
Posts: 36
Bill and other friends !
If i try to sum the discussion, would it be fair to say that " JAVA is more platform independent than any other language and we can make our program made in JAVA 100% platform independent by wrighting codesin the way of "Coding by contract" (as Heller says it). Like using Synchronized, priority and other tools, so that our program give same result where ever we run it.
Thanx.
laoniu gougou
Greenhorn

Joined: Jan 25, 2001
Posts: 25
Hi Bill:
In your reply on Feb 28, you said there are two threads: one is main and another is the thread you created. What do you mean the main? The main function or there is a hiden main thread on the schedular as default?
Now if the main method is not a thread, it is possible continue excetue before schedular try to determine who is next. If this is true, myMethod should alway execute first!
laoniu
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
I meant the main method:
public static void main( String args[] ) {}
This is a thread in and of itself. If you don't create any other threads, then this is the only one there.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread's run() sequence