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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:53:34+00:00 2026-05-23T00:53:34+00:00

i am trying to implement a multithreading in my app and i would like

  • 0

i am trying to implement a multithreading in my app and i would like to know how i can do this. Basically in a single view i have 2 blocks i want these two block to bounce around the screen however i want each block to be running on its own thread. So single view multi-thread…if that’s possible. Here’s what i have so far:

public class mainActivity extends Activity {


protected static final int GUIUPDATEIDENTIFIER = 0x101;

//Thread myRefreshThread = null;

BounceView myBounceView = null;

Handler myGUIUpdateHandler = new Handler() {

    @Override
    public void handleMessage(Message msg){
        switch (msg.what){
        case mainActivity.GUIUPDATEIDENTIFIER:
            myBounceView.invalidate();
            break;
        }
        super.handleMessage(msg);
    }
};


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    this.myBounceView = new BounceView(this);
    this.setContentView(this.myBounceView);

    new Thread (new RefreshRunner()).start();

}

class RefreshRunner implements Runnable {
    // @Override
    public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                    // Send Message to the Handler which will call the invalidate() method of the BoucneView
                    Message message = new Message();
                    message.what = mainActivity.GUIUPDATEIDENTIFIER;
                    mainActivity.this.myGUIUpdateHandler.sendMessage(message);

                    try {
                            Thread.sleep(100); // a 10th of a second
                    } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                    }
            }
    }
}


}

My BounceView is:

public class BounceView extends View {


protected Drawable mySprite;
protected Point mySpritePos = new Point(0,0);

protected enum HorizontalDirection {LEFT, RIGHT}
protected enum VerticalDirection {UP, DOWN}
protected HorizontalDirection myXDirection = HorizontalDirection.RIGHT;
protected VerticalDirection myYDirection = VerticalDirection.UP;


public BounceView(Context context) {
        super(context);

        this.mySprite = this.getResources().getDrawable(R.drawable.block);
}

@Override
protected void onDraw(Canvas canvas) {
        /* Check if the Ball started to leave
         * the screen on left or right side */
        if (mySpritePos.x >= this.getWidth() - mySprite.getBounds().width()) {
                this.myXDirection = HorizontalDirection.LEFT;
        } else if (mySpritePos.x <= 0) {
                this.myXDirection = HorizontalDirection.RIGHT;
        }

        /* Check if the Ball started to leave
         * the screen on bottom or upper side */
        if (mySpritePos.y >= this.getHeight() - mySprite.getBounds().height()) {
                this.myYDirection = VerticalDirection.UP;
        } else if (mySpritePos.y <= 0) {
                this.myYDirection = VerticalDirection.DOWN;
        }

        /* Move the ball left or right */
        if (this.myXDirection == HorizontalDirection.RIGHT) {
                this.mySpritePos.x += 10;
        } else {
                this.mySpritePos.x -= 10;
        }
        /* Move the ball up or down */
        if (this.myYDirection == VerticalDirection.DOWN) {
                this.mySpritePos.y += 10;
        } else {
                this.mySpritePos.y -= 10;
        }

        /* Set the location, where the sprite
         * will draw itself to the canvas */
        this.mySprite.setBounds(this.mySpritePos.x, this.mySpritePos.y,
                        this.mySpritePos.x + 50, this.mySpritePos.y + 50);

        /* Make the sprite draw itself to the canvas */
        this.mySprite.draw(canvas);
 }
}

So this works perfectly however this only handles a single block. I would like to add a secondary block (using the same graphic) that is handled by a separate thread. How can i do this? do i have to make a separate view for the other block?

  • 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-23T00:53:35+00:00Added an answer on May 23, 2026 at 12:53 am

    The looper that is used to read the messages you are sending from your thread is threadsafe but also sequences the received messages, meaning it can only be processing 1 msg at any given time. While you could, in theory, submit messages to this handler very easy from two separate threads, I’m not sure its going to accomplish what you want. I’m assuming you are seeking some sort of performance boost from using multiple threads?

    If you are trying to get a performance gain, there are two things working against you. In the above code, the UI updating is being done by a single thread. No matter how many threads you have sending messages to the myGUIUpdateHandler, only one message is being processed at a time. This may cause tons and tons of invalidates, but your entire scene is being redrawn. Second, you can only ever have one thread modifying Android UI elements. If you were hoping to have two threads independently redrawing two different boxes using a View, it is not possible. In fact, I don’t know if its possible to do so even using the opengl based views, since i think opengl also depends on a looper concept.

    My answer would be that it is not possible, unless you care to clarify why you need two threads so that I can try to form an alternative solution.

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

Sidebar

Related Questions

Trying to implement an autocomplete based on this It looks like it's very straight
Trying to implement a UITableView of names similar to the built-in Contacts iPhone app
im trying to implement a custom error page, what i want to be able
I’m buried in multithreading / parallelism documents, trying to figure out how to implement
For last 48 hours, I have been trying to understand Multithreading and Socket Programming
I've been experimenting with Clojure's multithreading features lately and trying to implement a simple
I'm trying to implement a server-client socket program in Java that can support multiple
I am trying implement Unblock me Puzzle. i want to change image position from
Trying to implement what I thought was a simple concept. I have a user
Trying to implement this gallery on my website. http://coffeescripter.com/code/ad-gallery/ It is noted in the

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.