• 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
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Thread Question

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if I implement both run and start methods?
Is run method ever called if I call start method as usual thread invoking?
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kaz
If you implement the start method that is override the start method then you are not putting a new thread into the ready state i.e. it would act like any other method and not function like the way the Thread.start() method should, unless you implicitly call the Thread's start() method within your overriden start() method. If you don't implement the run method then the Thread's class run method would be run which won't do anything but this is the case if you extend Thread if you implement Runnable you will have to implement run() method.
[ June 07, 2003: Message edited by: Anupam Sinha ]
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok
That was the last question of my exam. I guess I was right on that. Oh well, I don't know where I'm going from here.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kaz
I guess that wasn't your actual exam question. Actual exam questions can't be discussed in javaranch.
[ June 07, 2003: Message edited by: Anupam Sinha ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anupam Sinha:
Hi Kaz
If you implement the start method that is override the start method then you are not putting a new thread into the ready state i.e. it would act like any other method and not function like the way the Thread.start() method should, unless you implicitly call the Thread's start() method within your overriden start() method. If you don't implement the run method then the Thread's class run method would be run which won't do anything but this is the case if you extend Thread if you implement Runnable you will have to implement run() method.
[ June 07, 2003: Message edited by: Anupam Sinha ]


I have the following code snippet in which MyThread class has overriden start() and run() methods. The overridden start() calls Thread's start() but this does not not behave like a thread of execution. Instead it behaves like any other method call.
class MyThread extends Thread {
String[] sa;
public MyThread(String[] sa) {
this.sa = sa;
}
public void run() {
System.out.print(sa[0] + sa[1] + sa[2]);
}
public void start() {
new Thread().start();
}
}
class ThreadQuiz {
private static String[] sa = new String[]{"X","Y","Z"};
public static void main (String[] args) {
Thread t1 = new MyThread(sa);
t1.start();
sa[0] = "A";
sa[1] = "B";
sa[2] = "C";
}
}
The output should be "ABC" when running as a real thread.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Smita
Well when I said that the Thread's start() method needs to be called implicitly I didn't mean calling it via new Thread.start(). What I meant was a call using super.start(). Your thread is actually a new thread of execution but its not acting like that because it's running the run() method in the Thread class which does nothing.
 
A feeble attempt to tell you about our stuff that makes us money
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic