• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have come to believe that threads have a very unpredictable output.But what i want to know is what is the path they follow for executiom.what happens after i call run()

here is a sample problem.

class NewThread implements Runnable
{
Thread t;
NewThread()
{
t=new Thread(this,"Demo Thread");
System.out.println("Child thread: "+t);
t.start();
}

public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println("Child Thread: "+i);
Thread.sleep(1500);
}
}
catch(InterruptedException e)
{
System.out.println("Child Interrupted");
}
System.out.println("Exiting Child Thread");
}
}

class ThreadDemo
{
public static void main(String args[])
{
new NewThread();
try
{
for(int i=5;i>0;i--)
{
System.out.println("Main Thread: "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Main Thread Interrupted.");
}
System.out.println("Main thread Exiting.");
}
}






I am able to make threads but what i fail to understand is the process after start() is called
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right ... the exact behavior of a bunch of running threads is unpredictable. That's just part of the fun.

See Thread in the JavaDoc and follow a link to Thread.State. The JVM executes instructions on threads that are RUNNABLE. If there are several RUNNABLE threads, it uses some form of "time slicing" to run a few instructions from one, a few from another, then a few from another. Even if a thread remains RUNNABLE all kinds of things going on in the computer can influence how often it gets the CPU and for how long.

Does that answer any questions? Raise any new ones?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to emphasize, when Stan says "run a few instructions", that means something different to the computer than to you. you might see

system.out.println("this is one line of code");

but to the machine, that's many instructions. the JVM sees no problem with printing 3 characters, then stopping and moving to another thread, doing something there (including printing a few characters for that thread)... then coming back and printing a few more characters in this thread.
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope i understood.But what bothers me that i am not able to predict the first line of my output with surety.is this ok?
Do we have some rules for starting of execution at least in case of threads.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot predict which of two threads will run first. There are no rules are guidelines; you should not even try to predict, as the effort will be in vain.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the above responses saying you cannot predict the order in which different threads will run their tasks are of course true. However, this does not mean that you cannot have any control over the order of execution of code in different threads; it's just that you have to write code explicitly to ensure it.

The wait() and notify[All]() methods are particularly useful in allowing different threads to co-operate. For instance, one thread that needs a result being prepared by another thread can wait() until that other thread calls notify(), to say it has generated the result.
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i got it.Thanks guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic