Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3404604
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:24:12+00:00 2026-05-18T05:24:12+00:00

This is a Java syntax question, but just for some background. Using android, I

  • 0

This is a Java syntax question, but just for some background.

Using android, I created a small app that went really slow and often crashed because when a button was clicked the onClick() method changed button images for various buttons. It was really clunky and slow and I found out after a search on the net that the problem is pretty common, and when changing lots of images in the onClick() method, its best to put them in a separate thread. And some kind person gave the code for doing this.

new Thread(new Runnable() {
                public void run() {
                    quest.post(new Runnable() {
                        public void run() {
                            correct = nextQ();

                        }
                      });

                    }

                  }).start();

The nextQ() method and the “quest” TextView are mine. It’s not really relevant to my question what they do, but nextQ does a database search and updates images, and running it outside the thread is really slow and crashy.
Now I copied and pasted this code into my code and it runs fine. A happy ending. But I dont feel comfortable using code I don’t understand. And I don’t know Java very well. SO I researched as best I could anonymous inner classes, but I’m still stumped by the code.

So the anonymous class extends thread, and as it’s argument uses an anonymous class, implementing runnable, in which the “meat” of the code is placed..
Questions:
1)Why would I do this? I can understand using thread OR runnable, but why together?
2)How does this work? I went back to basics of thread and runnable, but I don’t see how you use them together this way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:24:12+00:00Added an answer on May 18, 2026 at 5:24 am
    • you are creating an anonymous implementation of the Runnable interface
    • you are passing that implementation to the constructor of the Thread class. So you are not extending Thread, you are just instantiating it, and pass an argument.
    • you then start() the thread. Thus the run() method of the passed argument will be invoked in a new thread.

    You need to do this, because:

    • the Runnable defines what gets executed
    • the Thread actually executes it.

    Your confusion is understandable, since a Thread is also Runnable. That’s why in Java 5 the executors framework was introduces, which separated the execution mechanism (an executor) from the executed code (Runnable or Callable).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize that this question might not make sense to some, but I was
I'm very new to Java programming language so this is probably dumb question but
sorry if this question is stupid, but I can't wrap my head around Java
This Java code compiles fine, but when I try to run it I get:
I'm using this java script library to show ratings http://www.fyneworks.com/jquery/star-rating/#tab-Overview . I'm using below
I have this java servlet that grabs information from a form, I need to
Hopefully someone knows the answer to this Java-Certification question: public static void main(String[] args)
I'm looking at some code with this form: 1 package com.stackoverflow.java.questions; 2 import java.util.ArrayList;
NOTE: This question is written in a C# like pseudo code, but I am
Is there anything that java cannot do? But other's can? I am talking about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.