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 7915707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:32:53+00:00 2026-06-03T14:32:53+00:00

I have a question related to the following link: What's the difference between Thread

  • 0

I have a question related to the following link: What's the difference between Thread start() and Runnable run()

In this question, I see a person creating runnable objects and then initializing them in two different ways. So, does this mean that you could pass these runnables around to other things at run time?

I want to pass code to a preexisting thread to be executed within that thread’s loop. I was looking around and from what I can tell, you would want to create a dedicated runnable class like the following:

    public class codetobesent implements Runnable  
     {  
       public void run()  
        {   
        ..morecodehere.  
        }  
      ...insertcodestuffhere  
     }  

But how would I pass this to a thread that is already running? Say I’m trying to make a game and I have something special I want the renderer to do in its thread. How would I pass this runnable to that thread and have it run this data correctly?

My current implementation of my rendering thread is the following, I pulled it off of a tutorial site, and it has worked pretty well so far. But I want to know how to pass things to it so I can run more than what’s just in the preset loop.

class RenderThread extends Thread 
{
private SurfaceHolder _curholder;
private UserView curview;
private boolean runrender = false; 

    public RenderThread (SurfaceHolder holder, UserView thisview)
    { //Constructor function - This gets called when you create a new instance of this object.
        curview = thisview;
        _curholder = holder;
    }

    public SurfaceHolder getThreadHolder()
    {
        return _curholder;
    }
    public void setRunning(boolean onoff) 
    {
        runrender = onoff;
    }
    @Override
    public void run() 
    {
        Canvas c;
        while (runrender)
        {
            c = null; //first clear the object buffer.
            try 
            {
              c = _curholder.lockCanvas(null); //lock the canvas so we can write to it
              synchronized (_curholder) 
              {//we sync the thread with the specified surfaceview via its surfaceholder.
                  curview.onDraw(c);
              }

            } 

            finally 
            {
              // do this in a finally so that if an exception is thrown
              // during the above, we don't leave the Surface in an
              // inconsistent state
                if (c != null) 
                {
                    _curholder.unlockCanvasAndPost(c);
                }
            }


        }

    }   



}
  • 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-06-03T14:32:55+00:00Added an answer on June 3, 2026 at 2:32 pm

    A Handler Thread implementation.

    private void testWorker(){
            WorkerThread worker = new WorkerThread();
            worker.start();
            for (int i = 0; i < 10; i++) {
                worker.doRunnable(new Runnable() {
                    public void run() {
                        Log.d("demo", "just demo");
                        try {
                            Thread.sleep(1000);//simulate long-duration operation.
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    };
                });
            }
        }
    
        private class WorkerThread extends HandlerThread implements Callback {
    
            private Handler mHandler;
    
            public WorkerThread() {
                super("Worker");
            }
    
            public void doRunnable(Runnable runnable) {
                if (mHandler == null) {
                    mHandler = new Handler(getLooper(), this);
                }
                Message msg = mHandler.obtainMessage(0, runnable);
                mHandler.sendMessage(msg);
            }
    
            @Override
            public boolean handleMessage(Message msg) {
                Runnable runnable = (Runnable) msg.obj;
                runnable.run();
                return true;
            }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to following thread. Prism RegionAdapter - Removing then Adding View
I have following event class. I have a question related to the Property method
My question is related to the command pattern, where we have the following abstraction
Related to my previous question , I have a string on the following format:
I have some thread-related questions, assuming the following code. Please ignore the possible inefficiency
I have a question related to this one . I don't want to do
I have a question related to this one : I'm trying to attach an
Not sure whether this is the right place, but I have a question related
Possible Duplicate: read iphone sms messages? I have following question related to get sms.
I have a question related to the following code snippet: class VarArgsTricky { static

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.