• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

threads

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
this is a very basic question but please clear my doubt.


output: Running

here e is an object reference or a thread.What is the difference between a thread and a object reference.We hav used e.start() so e should be a thread.Also when i run this program i got only one Running as an output but i hav started two threads.there should be two Running statements.plz explain
thanks in advance
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey manish
Basic difference is when you invoke a thread, it executes in a new thread without interrupting the current one. For detailed explaination see any of the text books (when exactly run() is called and why not when we call start() etc).
In your example, second one is just a Thread object which doesnt do anything( you did not create a Running object which extends Thread).

~SJCP
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, e.start() is to excute Example.run(), while t.start() is to excute Thread.run().
So, for Thread t, it will print out nothing if you haven't rewritten the method run().
The code below will give you the second statements:
---------------------------------
Thread t = new Thread(){
public void run(){
System.out.println("Hello2!");
}
};
t.start();
[ August 22, 2004: Message edited by: Marlon Lee ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've changed your code here slightly to demonstrate something...



By sending the Thread class a runnable target ...Thread t = new Thread(e); you will then have two separate threads running. I believe the Thread class implements Runnable itself - which is why this works.

Steve
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.Thread class and has a method start() that creates a thread and calls the run() method declared and defined in the thread class.

when you extend this class, override the method run() and call start() as you did in your program - a new thread is created and the overridden method run() is called hence you get "Running" as output.

in case of the statement
Thread t=new Thread()

you created an instance of the thread class and called the start() method which inturn called the default implementation of run() method (which does nothing).

Hope this helps

Kanag
 
Legend has it that if you rub the right tiny ad, a genie comes out.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic