• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Threads

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code:
class Thread1
{
public static void main(String args[])
{
run();
}

public void run()
{
System.out.println("inside run()");
}

class Thread2 extends Thread1
{
Thread2 t2 = new Thread2();
t2.run();
}
public void run()
{
System.out.println("inside subrun()");
}

}

We know that "threads" are objects in the java language and the "run()" method should be "overridden" and should have a code that will be executed
by the new thread .This method must be public with a void return type and
should not take any arguments.

So if above passage is true y cant i get the output as "inside subrun()" when i can "override" run method from base class

tx
venkat
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest that you reread the chapter on Threads in your book.

In the code you posted:
Thread1 must extend java.lang.Thread
The code creating a Thread2 object is in a Thread2 instance initializer, so it won't ever be executed.
You don't call run() directly to start a new thread. You call start() on the Thread object.

By the way, run() can have either default or public access, as you choose.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to be careful with your braces. The way your code is presented, Thread2 is actually a member of Thread1 (as well as a subclass), and Thread1 has 2 run methods. Indeed, the second run method is not inside the subclass, as it announces.

Also:
  • If you want these to be Thread objects, then the superclass should extend Thread.
  • In main, you'll need to create an instance before calling run(), and you probably want to call start() instead.
  • The call to t2.run() needs to be inside a method, although you probably want to call start() instead.



  • See this post for start() vs. run() details:
    https://coderanch.com/t/247406/java-programmer-SCJP/certification/consider-code-threads

    You might also want to work through Bruce Eckel's Thinking in Java chapter on threads:
    http://www.faqs.org/docs/think_java/TIJ315.htm
    [ January 05, 2005: Message edited by: marc weber ]
     
    Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic