• 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 problem

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


SIr i have a doubt that

the above code is giving gogogo as the output but sir it is calling the method run() of the thread class ...how it is giving this..
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's perfectly valid to call the run method just like any other regular public method. However, it's important to understand that a new thread is not being created while calling run. It is simply the main thread (the tread running the main method) who is calling run on the t object. That's what's happening on the first 2 calls of run (hence, the first two "go"). On t.start() a new thread is being created, and once the JVM selects it to be on the Runnable state it executes the run method (thus, the last "go"). Also, the Thread constructor you are using is setting the target to Cruiser object. You're specifically stating the object whose run method is called.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

narendra bhattacharya wrote:
the above code is giving gogogo as the output but sir it is calling the method run() of the thread class ...how it is giving this..



The default run() method, is to check for a runnable object, and then to call it. This is why when you call the thread start() method, it will eventually get to your runnable objects run() method.

Henry
 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually we are running three Threads tow of Thread class and one of main() method thread
So when we start the Thread by it runs Three Threads and thus the output is gogogo.
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it runs Three Threads and thus the output is gogogo.



there are just two threads the 1. main 2.from ThreadClass.
first and second go is printed by simple method call t.run() (it has nothing to do with threading concept).
third go is printed when start method calls run of thread.

-P
 
Javier Cortes
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praful Thakare wrote:

it runs Three Threads and thus the output is gogogo.



there are just two threads the 1. main 2.from ThreadClass.
first and second go is printed by simple method call t.run() (it has nothing to do with threading concept).
third go is printed when start method calls run of thread.

-P



...as stated on the second post. However, I'd like to correct that I meant "...and once the JVM selects it to be on the Running state..." instead of "...and once the JVM selects it to be on the Runnable state...". Sorry for that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic