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

  • Home
  • SEARCH
  • 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 7610121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:20:35+00:00 2026-05-31T01:20:35+00:00

What is the right way to get an integer value back and forth between

  • 0

What is the right way to get an integer value back and forth between threads?

I have two threads running a run(){} function. One is receiving/sending USB messages and one is running a game UI. They both work independently. Modifying the value of the game with the data from the USB is the problem area.

What I’ve tried so far: I tried creating a global object called container which contained a public int variable and passing that to both the threads to communicate, but it crashes the program upon running.

Edit: Can I save the file to the SD card and have the other thread view it? Thread 1 will need to write to the SD card and thread 2 will need to read it. There is no need for two way comm.

edit: If there’s a way to create an application wide, global variable of type int, or an application wide object containing an int, that will work. If doing that exists, I’d like to do it that 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-31T01:20:36+00:00Added an answer on May 31, 2026 at 1:20 am

    You should probably stick to the Android threading model. See this for example.

    The UI thread is the main thread in Android. If your UI thread is not the main thread, you did something wrong.

    From the link above, don’t use AsyncTask since you need something that modifies the UI more than once (I presume). Call instead

    myActivity.runOnUIThread(new Runnable() {
       @Override
       public void run() {
           someUIelement.setValue(someIntReadFromUSB);       
       }
    }
    

    from your thread that reads USB. When you declare that Runnable you’ll need to have someUIElement and someIntReadFromUSB in the scope. If they are local variables in that scope, you’ll have to make sure they are final variables. (If you do have to declare the integer final, you can just declare a new final variable right before you define the Runnable and copy the value you want to send to it.)

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

Sidebar

Related Questions

I am trying to find the right way to get the data from a
What is the right (portable, stable) way to get the ToS byte of a
What's the right way to control timeouts, from the client, when running against a
I'm trying to get the maximum value of an integer field in a table.
I have a calc function in java script that takes three integer parameters, following
Is there a way in Python, to have more than one constructor or more
I have two integers that I want to divide to get a percentage. This
What is the right way to populate the model for the index page in
What is the right way to perform some static finallization? There is no static
What's the right way to go about saving data like Pages, Numbers and other

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.