• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Thread

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i got it.Thanks guys.
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic