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

    Default Concept of Threads, main thread etc

    Java Code:
    public void onClick(View v){
    new Thread (new Runnable(){
    public void run(){
    Bitmap b = loadImageFromNetwork("http://example.com/image.png");
    mImageView.setImageBitmap(b);
    }
    }).start();
    }
    In this code, there is just a single thread object being created. I have read that in this code, the UI toolkit is being accessed and monitored from outside the main/UI thread. But how do we know that this is the outside of the main/UI thread. I can see only one thread being started.

    and then it is this anonymous class which contains the implementation of run().

    How do we just get to know that this thread is not the main/UI thread, and that there is some other main/UI thread, in which lies the mImageView, which is being accessed from the implementation of run() ?

    An answer is coming to my mind but I am not sure of it, i.e. no UI resources are loaded in this thread, so this is not the UI thread, but the UI thread is somewhere else, and we are accessing its member mImageView and manipulating it from this thread.
    Last edited by Nazneen Ali; 11-12-2013 at 11:02 PM. Reason: added last two lines
    Don't forget to smile :-)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Concept of Threads, main thread etc

    Try System.out.println(Thread.currentThread().getName( )). Place it in main and in the thread and see what happens. Also note that you can change the name of the thread by supplying a String to the second argument of the new Thread call.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Concept of Threads, main thread etc

    There must be at least one other thread, which is the thread that runs this code. Whatever thread that might be, one thing is certain: the thread being created and started in this code is not the UI thread, because you don't create and start that yourself.
    Get in the habit of using standard Java naming conventions!

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Concept of Threads, main thread etc

    BTW, if this is in Android, keep in mind that the main thread and the UI thread are different names for the same thread. In standard Java (using AWT, Swing, etc.) the main thread and UI thread are different threads.
    Get in the habit of using standard Java naming conventions!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Concept of Threads, main thread etc

    Interesting tidbit. I never considered Android (I don't do Android -- yet). But it seems different names for the same thread could get confusing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Concept of Threads, main thread etc

    Just to be clear: I forget what the actual name of the main thread is, but it only has one name. What I mean is that Android books and even the official documentation use the terms "the UI thread" and "the main thread" interchangeably, because the UI thread is the main thread. In Android, there is no equivalent to the thread named "main" in J2SE. The entry points to your code are all called in the UI thread.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Exception in thread main java.lang.nosuchmethoderror main
    By prikesh_joshi7 in forum Java Applets
    Replies: 3
    Last Post: 01-26-2012, 04:19 PM
  2. Replies: 1
    Last Post: 11-23-2011, 05:52 AM
  3. Replies: 1
    Last Post: 01-23-2011, 05:19 PM
  4. Need help in thread concept
    By umapathy_sekar in forum Advanced Java
    Replies: 2
    Last Post: 11-11-2010, 06:25 AM
  5. Thread concept
    By javahelp00 in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-01-2009, 05:15 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
  •