File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
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


Jitendra
SCJP1.5
SCWCD1.5
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
I think i got it.Thanks guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
Thread qstn
Threads problem !!!!
Threads
What are those two threads???
Threads