Results 1 to 7 of 7
  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default a way for creating a Thread

    Hello
    to what does "this" point in following code?

    Java Code:
    import java.lang.Runnable;
    
    class Counter implements Runnable {
    	
    
          public int count = 1;
          
          Counter()
          {
          	Thread t=new Thread([B]this[/B]);
          	t.start();
          }
          
          public void run()
          {
              while (count < 3)
              {
                  try
                  {
                  	System.out.println(count);
                    Thread.sleep(2000);
                  } 
                  catch (InterruptedException e)
                  {
                  	System.out.println("InterruptedException"); 
                  }
                  count++;
            }
            System.out.println("+");  
        }
      } 
    
    public class B
    {
    	public static void main(String args[])
    	{
    		new Counter();
    		Thread.sleep(1000);
    	}
    }

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

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    That 'this' refers to the Counter object you are creating at that moment, no secrets nor magic involved. As far as a Thread is involved, it is only interested in that Counter object as a Runnable.

    kind regards,

    Jos

  4. #4
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    From stackoverflow site.
    Question: is it legal for a thread to call this.start() inside its own constructor? and if so what potential issues can this cause? I understand that the object wont have fully initialized until the constructor has run to completion but aside from this are there any other issues?
    Answer: For memory-safety reasons, you shouldn't expose a reference to an object or that object's fields to another thread from within its constructor. Assuming that your custom thread has instance variables, by starting it form within the constructor, you are guaranteed to violate the Java Memory Model guidelines. See Brian Goetz's Safe Construction Techniques for more info.
    AND
    From Bruce[1].Eckel.Thinking.In.Java.4th.Edition.Dec.2007.eBook-BBL
    Notice that start() is called within the constructor. This example is quite simple and therefore probably safe, but you should be aware that starting threads inside a constructor can be quite problematic,because another task might start executing before the constructor has completed, which means the task may be able to access the object in an unstable state.This is yet another reason to prefer the use of Executors to the explicit creation of Thread objects.
    Hello
    Thank you about your help and guidance.
    Please see two quotes and then answer following question.

    Thus, can we say that KIND ONE is better than KIND TWO?
    Java Code:
    KIND ONE:
    public class B
    {
    	public static void main(String args[]) 
    	{
    		Counter  r = new Counter();
    		Thread   t = new Thread(r,"CounterThread");
    		t.start();	
    	}
    }
    Java Code:
    KIND TWO:
          Counter()
          {
          	Thread t=new Thread(this,"CounterThread");
          	t.start();
          }
    
    public class B
    {
    	public static void main(String args[]) 
    	{
    		new Counter();
    	}
    }

  5. #5
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    I described badly?

  6. #6
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    what does better mean here..does it relate to efficiency or ..?

  7. #7
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    I do not have right realization about this subject. I also like to know which one is true or better? or the best manner is which one?

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. Creating a thread that modifies UI
    By prueba2306 in forum Eclipse
    Replies: 2
    Last Post: 08-01-2008, 01:26 PM
  3. Creating a Thread using the Runnable interface
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:31 PM

Posting Permissions

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