• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

thread EXAM WATCH from K&B

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i could not understand the following exam watch from threads chapter:

There is nothing special about run() method as far as Java is concerned.Like main(), it just happens to be the name( and signature) of the method that the new thread knows to invoke.So if you see code that calls the run() method on a Runnable(or on a Thread instance), it is perfectly legal.But it does not mean that the run() method will run in a separate thread!Calling a run() method just means you are invoking a method from whatever thread is currently executing, and the run method goes onto the current call stack rather than at the beginning of the new call stack.The following thread does not start a new thread of execution:

Runnable a = new Runnable();
a.run()//legal, but does not start a separate thread

please explain the above and also let me know how can this:

Runnable a = new Runnable();

be legal. As far as i know the interfaces are abstract and that is why can not be instantiated.
i am confused
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey

pattnayak
---------------------------------------
Runnable a = new Runnable();
--------------------------------------

See errata

There is a mistake here change to
Thread a=new Thread();

And some other changes are also there in the book.So check errata once.

Thanks
Anil Kumar
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi debasmita,
Look at the following code

#Case1
public class Test implements Runnable
{
public static void main(String[] args)
{
Thread t=new Thread(new Test());
t.start();
}
public void run()
{
for(int i=0;i<9;i++)
{
System.out.println(i);
}
}
}
Here in the above program you are calling the run() method by starting the thread that is t.start() when you do this a seperate stack will be created.Look below

Stack "1" : main() // main in one stack and when start method is called one more stack will be created where the thread runs seperately.

Stack "2" : run // one more stack will be created for the run method.

#case 2
public class Test implements Runnable
{
public static void main(String[] args)
{
Test tst=new Test();
t.run();
}
public void run()
{
for(int i=0;i<9;i++)
{
System.out.println(i);
}
}
}

In the above code run() method is called directly so new stack will be not created.Look below

Stack "1" : main() // when the run() method will be called it will run in the same stack. See below

Stack "2" :run()
main()
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks anil and nik for the info and explaination
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic