Results 1 to 3 of 3
  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Starting threads by implementing Runnable

    Java Code:
    //The code is for an android app, so no main(), but the question falls under the category of java.
    public void onClick(View v){
    new Thread(new Runnable() {
    public void run(){...}
    }).start()
    }
    This code starts a thread.
    Runnable is an interface with the only method run(). A variation/form/whatever of the above code, which is understandable for me is:

    Java Code:
    public void run(){...} 
    
    public static void main(String [] args){
    new Thread (new ClassImplementingRunnable()).start()
    }
    In this piece of code, the start() method is a nonstatic method of class Thread, so called by an object of Thread, and an object of the class implementing Runnable (i.e. implementing run() which contains the code to be executed in the thread) is passed to the constructor of Thread during the creation of the Thread object. I understand.

    But the code given earlier is a bit peculiar for me, particularly the argument passed to the Thread constructor.
    What I understand is: new Runnable() is creating an object of the class implementing Runnable (we can use interface types in place of class names implementing them to create objects of the class, and then is the the block of code containing the implementation of the run() method of Runnable


    1. What is this block of code? Is this block of code just meant to contain the implementation of run(), or is it the implementation of all the nonstatic methods of the class and the methods of the interfaces the class implements, or the implementation of all the methods of the class?
    Don't forget to smile :-)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Starting threads by implementing Runnable

    Yep, that construct is named an 'anonymous class' and the block of code is the definition of the anonymous class; if that class extends an interface, it has to define all methods in the interface; if that class extends another class it has to implement the non-implemented methods if the super class is an abstract class; otherwise it can only override method(s) from the super class because there is no way to call any freshly created method in the anonymous class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Starting threads by implementing Runnable

    Thank you very much @JosAH
    Don't forget to smile :-)

Similar Threads

  1. Question about a class implementing Runnable
    By d3n1s in forum Advanced Java
    Replies: 12
    Last Post: 06-18-2011, 06:16 PM
  2. problems with threads in my program (runnable)
    By runn81 in forum Threads and Synchronization
    Replies: 12
    Last Post: 05-09-2011, 05:26 PM
  3. Replies: 2
    Last Post: 02-14-2011, 02:27 PM

Tags for this Thread

Posting Permissions

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