• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Question

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw this question in a book at the bookstore. I thought a thread was invoked with the start() method, not the run() method. However, all the choices compile and run an infinite loop. What's the answer and why?
1. public class T implements Runnable {
2. public static void main (String[] args) {
3. //insert code here
4. }
5.
6. public void run() {
7. int i=0, j=0;
8. for (; {
9. i++;
10. j++;
11. System.out.println(i+", "+j);
12. }
13. }
14. }
You want to cause execution of the run method in a new thread of execution. Which line(s) should be added to the main method in line 3?
A. Thread t = new Thread(T).start();
B. Thread t = new Thread(T).run();
C. T t = new T();
new Thread(t).start;
D. T t = new T();
new Thread(t).run;
E. T t = new T();
t.run();
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you call run() you are calling the run() method, but not in a Thread. If you call start() then a thread is created.
With Threads, it's a good idea to narrow down the rest, think about what type of Thread you create with a Runnable and what type you create by extending a Thread. That ought to help a bit... Then, see if you need what kind of target(t or T or both?) you've got and you should get an
answer(s).
They will all compile, but which will give you a new thread! Try using some of the methods of thread like currentThread etc to see what is really happening behind the scenes too.
Alan Phillips
SCJP 2
[ September 17, 2002: Message edited by: Alan Phillips ]
[ September 17, 2002: Message edited by: Alan Phillips ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think answer is C. But threads is not my Forte...
[ September 17, 2002: Message edited by: Barkat Mardhani ]
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barkat Mardhani:
I think answer is C. But threads is not my Forte...


It should be start() in answer C.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yan:

It should be start() in answer C.


I am not following you. I see start() in the second line of C option.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike said:

I thought a thread was invoked with the start() method, not the run() method. However, all the choices compile and run an infinite loop.


You are correct Mike. However, Thread has a run() method which can be overridden by subclasses and
invoked like any other method. The implementation in Thread does nothing.
The invoking of run() in the questions is an attempt to test whether you understand the different effects of invoking start() (which causes run() to be invoked in the new thread's context) and calling run() directly which executes in the current thread's context.
-Barry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic