• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

one more thread

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread t = new Thread(this);
t.start(); (when used in static context we get a compile time error)
(when used in a method it runs fine)
but i read some where that Thread constructor has no constructor like Thread(this) is it true if so why is the method runnig fine.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:
I guess the reason for compilation error in Static context is that, The static methods don't have "this" reference. Since the static members of a class are not associated with any object instead they are of class only. hence, they can't have "this" reference.

Whereas when u call it in a non static method, u can easily pass "this" to the threads constuctor. As that methods must have "this" reference.

About the threads Constructor, I can't say much until I see the code...If "this" is refering to a runnable target then u can surely pass it in threads constructor...



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

I suppose you are writing the code somewhat like this.



i.e. the class that you have declared is extending the "Thread" class or it is implementing the Runnable interface. In that case it is perfectly legal to write this Thread newT= new Thread(this); beacuse the "this" in the above code refers to your class and in the "Thread" class we have a constructor that takes a Thread argument or Runnable target.

Hope that helps
Asha
 
srilatha annepu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i misread
it says that there is no this variable in static main method to pass to the thread constructor.any way discussing with u cleared my doubt and also understand it clearly.thanks
 
pie. 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
reply
    Bookmark Topic Watch Topic
  • New Topic