Results 1 to 5 of 5

Thread: I Need help!!!

  1. #1
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default I Need help!!!

    Thread th= new Thread(this);

    th.start();


    Can someone tell me what does that "this" mean in this context ???
    I would appreciate if anybody show me an example also why we need to add that "this" on brackets and what happened if we dont write "this" inside the brackets

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by lulzim View Post
    Thread th= new Thread(this);

    th.start();


    Can someone tell me what does that "this" mean in this context ???
    This means the current object, the one for which this code is being called on, just the same as it means in most all other contexts (except when talking about constructors calling other constructors).


    I would appreciate if anybody show me an example also why we need to add that "this" on brackets and what happened if we dont write "this" inside the brackets
    I have no idea what you're asking here.

    Also, you will likely get more folks helping you if you use a smarter thread title. Of course you need help, else you wouldn't be asking a question, but wouldn't it make more sense to state as your title a very brief summary of your problem? And with perhaps fewer exclamation marks (none would be best of all).

  3. #3
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    the current object, which object actually is this current ?? is that "th" wich i have created as instance of class Thread???

    My question was if we dont put that "this" token or what u call it what would happen and why we have to use this inside the brackets???

    I think i clear now

  4. #4
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    Ohh i got the meaning, its like calling the instances of class which helps to avoid name conflicts from the methods arguments.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Normally you need to pass a runnable into the constructor of the thread. So you refer to this, which is a runnable(or should be).

    Java Code:
    public class X implements Runnable{
      private int y;
      public X(){
        this.y = 0;
      }
      public void run(){
        System.out.println("In class x's run method");
      }
      public void doSomething(){
        Thread th = new Thread(this);
        th.start();
      }
    }
    In this snippet, the this keyword passes in the class being worked in.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •