• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
laoniu gougou
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 219
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
nitin sharma
Ranch Hand
Posts: 290
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Andy!
 
James Du
Ranch Hand
Posts: 186
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one suggest a good book for samples/examples in multithreading
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic