class Newthread implements Runnable{

String name;
Thread t;
Newthread (String threadname)
{
            name=threadname;
            t=new Thread(this,name);
            System.out.println(t);
            t.start();
}

public void run()
{
      try
         {   for(int i=5;i>0;i--)
             {
                      System.out.println(i);
                      Thread.sleep(1000);
             }
             
          }catch(--------){
           --------}
}

}

class A{
  public static void main(){

  new Newthread("one");
  new Newthread("two");
  new Newthread("three");

  try{
     Thread.sleep(10000);
     }catch(){}
}

in this code after calling constructor, why not start function starts executing rather than going back to main class(A here)? why the statement t.start(); not jump to run function? it first goes to main class and then come back and start executing run function of all three. why it is so ? explain me please i m not getting it. :-(

Recommended Answers

All 18 Replies

why not start method starts executing rather than going back to main class(A here)?

When you call start() for a Thread you tell the OS to put the referenced thread in the queue of threads that it maintains. When the OS decides, it will chose one of the threads from the queue of threads and let it execute.
The thread that called the start() method will continue executing. At some point it will give control back to the OS. At that time the OS will look in the queue of threads to see which thread it can give control to next.

If you want the newly created thread to start immediately, try calling sleep or yield to give up control and let the OS chose what thread to start.

When you call start() for a Thread you tell the OS to put the referenced thread in the queue of threads that it maintains. When the OS decides, it will chose one of the threads from the queue of threads and let it execute.
The thread that called the start() method will continue executing. At some point it will give control back to the OS. At that time the OS will look in the queue of threads to see which thread it can give control to next.

If you want the newly created thread to start immediately, try calling sleep or yield to give up control and let the OS chose what thread to start.

ok ! when i call start it got in queue. i agreed.but when it starts executing ? examples i have seen, in that examples the new thread starts executing when main thread goes to sleep.
U said "at some point it will give control back" in this line what is "at some point"? what is that point ? please explain me once! please!

When you call start() for a Thread you tell the OS to put the referenced thread in the queue of threads that it maintains. When the OS decides, it will chose one of the threads from the queue of threads and let it execute.
The thread that called the start() method will continue executing. At some point it will give control back to the OS. At that time the OS will look in the queue of threads to see which thread it can give control to next.

If you want the newly created thread to start immediately, try calling sleep or yield to give up control and let the OS chose what thread to start.

let i have made 5 threads.all are now in queue as you said.okay! now,you said the method which called the start method will continue to executing.my question here is that all 5 threads has called up start method,then which method will continue to executing as you said?

which method will continue to executing as you said?

That is up to the OS to decide which thread to start next and when to start it.
You can set a thread's priority to tell the OS which threads you would like to have control first.

That is up to the OS to decide which thread to start next and when to start it.
You can set a thread's priority to tell the OS which threads you would like to have control first.

ok ! when i call start it got in queue. i agreed.but when it starts executing ? examples i have seen, in that examples the new thread starts executing when main thread goes to sleep.
U said "at some point it will give control back" in this line what is "at some point"? what is that point ? please explain me once! please!

The control of which thread executes and when it executes is up to the OS.
A thread can yield control voluntarily by calling sleep and some other methods.

That is up to the OS to decide which thread to start next and when to start it.
You can set a thread's priority to tell the OS which threads you would like to have control first.

please tell me as u said "at some point". what is that point? when will it give control back to OS ?/ please read my all questions then answer once.it will help us both

"at some point". what is that point?

Let me give an example of what I mean when I say "at some point".
If I throw a ball in the air, at some point the ball will fall to the ground. When that happens will depend on how high I throw the ball.

Let me give an example of what I mean when I say "at some point".
If I throw a ball in the air, at some point the ball will fall to the ground. When that happens will depend on how high I throw the ball.

hmm! i m not able to relate this example with threads sorry to say! okay! give me one code example please for understanding this concept.

Also remember you may be executing on a machine with multiple CPUs, each of which may support more than one simultaneous thread. So if you're on a top-of-the-range quad-core hyperthreaded desktop the answer is that your main thread and all three new threads could all execute at exactly the same time.
Note also that it's not necessary to give control back; all modern operating systems will suspend a thread and give its processor to another thread based on algorithms inside the OS.
Norm is saying "at some point" because all you know is that it will happen, sometime. The OS will decide when, depending on many factors outside your control or knowledge.

Also remember you may be executing on a machine with multiple CPUs, each of which may support more than one simultaneous thread. So if you're on a top-of-the-range quad-core hyperthreaded desktop the answer is that your main thread and all three new threads could all execute at exactly the same time.
Note also that it's not necessary to give control back; all modern operating systems will suspend a thread and give its processor to another thread based on algorithms inside the OS.
Norm is saying "at some point" because all you know is that it will happen, sometime. The OS will decide when, depending on many factors outside your control or knowledge.

again one question smashing in my mind now! okay! let i have called o1.t.start(); and second statement is o2.t.start();(t is object of Thread class here and o1 and o2 are my objects of class implementing Runnable).now in this example i have seen is that both statement are executing give control to each other randomly.if in run(), i give counter then its value is randomly decided means that loop is iterating on none basis.both threads are giving control to each other without any reason.(both have same priorities). how ill u explain this ?

If you have two or more threads with the same priority then control may change between them at any time (or they may all have control at he same time if you have multiple CPUs). There is always a reason behind when and why control is taken/given, but it is outside your control and you can't predict it.
This is why Java has things like synchronised blocks and the java.util.concurrent package. They are there so, if you want to, you can control what gets executed when.

If you have two or more threads with the same priority then control may change between them at any time (or they may all have control at he same time if you have multiple CPUs). There is always a reason behind when and why control is taken/given, but it is outside your control and you can't predict it.
This is why Java has things like synchronised blocks and the java.util.concurrent package. They are there so, if you want to, you can control what gets executed when.

but in case of synchronised also, sometimes even after using that output is not that which we want. i know you are getting what i want to ask! one more question! when more than one thread having same priority is in queue, then they will exchange control on none basis. what m saying is right ?

they will exchange control on none basis.

Its up to the OS to decide what thread has control.
What does "none" mean in you question?

Its up to the OS to decide what thread has control.
What does "none" mean in you question?

none means without any reason they are giving their controls to each other. means randomly they are doing that.

Its up to the OS to decide what thread has control.
What does "none" mean in you question?

okay! let i have called o1.t.start(); and second statement is o2.t.start();(t is object of Thread class here and o1 and o2 are my objects of class implementing Runnable).now in this example i have seen is that both statement are executing give control to each other randomly.if in run(), i give counter then its value is randomly decided means that loop is iterating on none basis.both threads are giving control to each other without any reason.(both have same priorities). how ill u explain this ?

The threads do not "give control to each other randomly". The Operating System gives control to neither, or one of them, or all of them, according to its own rules.

okay! let i have called o1.t.start(); and second statement is o2.t.start();(t is object of Thread class here and o1 and o2 are my objects of class implementing Runnable).now in this example i have seen is that both statement are executing give control to each other randomly.if in run(), i give counter then its value is randomly decided means that loop is iterating on none basis.both threads are giving control to each other without any reason.(both have same priorities). how ill u explain this ?

Your question isn't very clear, you need to present your question with a small self contained program which displays the behaviour you are observing. Plus, you can't invoke the start() method of the Thread multiple times which is what you seem to be doing in your query which implies that there is some explanation missing. Post code so that we are all on the same page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.