aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread EXAM WATCH from K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread EXAM WATCH from K&B" Watch "thread EXAM WATCH from K&B" New topic
Author

thread EXAM WATCH from K&B

debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
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


debasmita
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
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

Joined: Apr 12, 2007
Posts: 94
thanks anil and nik for the info and explaination
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread EXAM WATCH from K&B