wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

laoniu gougou
Greenhorn

Joined: Jan 25, 2001
Posts: 25
class MyThread extends Thread{
String str;
public MyThread(String s){str = s;}
public void run(){System.out.println(str);}
public static void main(String args[])
{
MyThread s1 = new MyThread("t1");
MyThread s2 = new MyThread("t2");
s1.start();
s2.start();
System.out.println("Main");
}

1)Could the output Main before t1/t2, or Main is definitely after
t1/t2 (t1, t2 order can not be determined)?
2)Can anybody explain whether main() method is not running in a Thread or not? If it is, what is its priority?
sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
Hi Laoniu
Originally posted by laoniu gougou:
class MyThread extends Thread{
String str;
public MyThread(String s){str = s;}
public void run(){System.out.println(str);}
public static void main(String args[])
{
MyThread s1 = new MyThread("t1");
MyThread s2 = new MyThread("t2");
s1.start();
s2.start();
System.out.println("Main");
}

1)Could the output Main before t1/t2, or Main is definitely after
t1/t2 (t1, t2 order can not be determined)?
2)Can anybody explain whether main() method is not running in a Thread or not? If it is, what is its priority?

For 1. I think the output Main would be before t1/t2, I cant compile the code right now and confirm that.
For 2. Yes, the main method is itself executed in a thread.
The thread priority would be a default priority which is
a final in the Thread class. (public static final int NORM_PRIORITY).
Hope that helps .
Sajida
preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
i am unable to understand why main gets printed first when other two threads have already started and we cannot be sure of how threads are going to behave coz of their priorities!!!
somebody please explain
thank you
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
When you start your threads they go off to the run() method and start execution there. Your main method doesnt have that extra overhead to worry about and so the main thread gets to its print statement first. I compiled and confirmed it.
Plus i think that thread scheduling has something to do with it. The OS im on now(win 2k) uses time slicing. So that means that each thread gets a certain amount of time to do its deed. I might be a bit off though, someone else help!
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Yes you are correct Andy. A thread inherits its priority from the thread that called it. In this case, both threads are spawned from the main() method, so they inherit its priority. Thus the order of execution is completely up to the OS running the JVM. And since main() has only one line of code left to execute after starting the other two threads, it will likely output first every time this program is run.
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
class MyThread extends Thread
{
String str;
public MyThread(String s)
{
str = s;
}
public void run()
{
for(int i=0;i<30;i++)
{
System.out.println(str);
}
}
public static void main(String args[])
{
MyThread s1 = new MyThread("t1");
MyThread s2 = new MyThread("t2");
s1.start();
s2.start();
System.out.println("Main");
System.out.println("Main1");
System.out.println("Main2");
System.out.println("Main3");
System.out.println("Main4");
System.out.println("Main5");
System.out.println("Main6");
System.out.println("Main7");
System.out.println("Main8");
}
}
In the above given code i have just made a bit of modificaion to show that we cannot determine the behaviour of the thread scheduler.
In the give programme,line calling the start method does not start the thread immediately it register's the thread with the thread scheduler, it's now totally upto the scheduler wheteher it start's the thread immediately or at some point later, After registering the thread with thread scheduler's, the call to the run method return's,therefore execute's the line following the start() method.
U cannot gaurantee the behaviour, just compile the given code and see the output carefully.
laoniu gougou
Greenhorn

Joined: Jan 25, 2001
Posts: 25
OK, guys, we still not quite clear. As Sajida said all three thread (ti, t2 and mail()) have the same priority, shouldn't the thread scheduler let one thread finish its own job before let another thread run. In such case, main will always output first.
Anybody can classify it?
Thanks
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
Thats where the OS thread scheduler comes in. In a windows environment, time slicing is used. Each thread gets a slice of processor time to do its job. Its different on different platforms. Some OS's like unix i believe dont use time slicing, so the result will sometimes be different.
laoniu gougou
Greenhorn

Joined: Jan 25, 2001
Posts: 25
Thanks, Andy!
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
you're right, nitin
No one can guarantee the order the thread scheduler let the threads run.
Given the first code, If we try to create more instances of MyThread and start them before the println("Main") statement, there's the chance that "Main" is not the first output line.
the following code is what I inserted before the println("Main") statement.

[This message has been edited by James Du (edited March 30, 2001).]
rahatekarms
Greenhorn

Joined: Mar 30, 2001
Posts: 1
can any one suggest a good book for samples/examples in multithreading
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
"rahatekarms"
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention which you agreed to when you registered, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please create a new log in with a new name which meets the requirements.
Thanks.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
Threads-synchronized blocks,Valentine &Michael and Others,Help needed
Thread example, dont know why is it a deadlock condition
Deadlocks in Threads?
help regarding synchronized methods
Threading synchronization