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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:57:57+00:00 2026-06-10T09:57:57+00:00

My thread is not working properly. It is executing all the code in RepeatingThread()

  • 0

My thread is not working properly. It is executing all the code in RepeatingThread() method but not in the run() method every 3 seconds. What am I doing wrong?

Here is the the code:

public RepeatingThread rt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.e(LOG_TAG, "Start Repeat Thread");
    //rt = new Thread(new RepeatingThread());
    rt = new RepeatingThread();
    rt.start();
    Log.e(LOG_TAG, "Started Repeat Thread");
}

public class RepeatingThread implements Runnable {

    private final Handler mHandler = new Handler();
    private int len = 0; 


    private byte[] input = new byte[len];


    public RepeatingThread() {
        //try {
        Log.e(LOG_TAG, "Before inputJSON String");
        //inputJSON = dataInputStream.readUTF();
        //URL url = new URL("tcp://23.23.175.213:1337");
        //inputJSON = dataInputStream.readUTF();
        //inputstrrd = new InputStreamReader(socket.getInputStream());
        String hello = "hello world";
        //String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream()));

        //Convert 
        Log.e(LOG_TAG, "After inputJSON String:" + inputJSON); 
        /*}
         catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }*/

        //LOOK HERE FIRST  
        //inputJSON is what is received back from the server - Take the inputJSON 
        //String and use regular expressions HERE to remove all the other characters in the 
        //string except the payload JSON.
        //refreshViewModels(inputJSON);
    }

    @Override
    public void run() { 
        try {
            Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON");
            //outputstrwr.write(outputJSONserv);  //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
            //inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
            inputJSON = ConvertByteArrayToString(getFileBytes(inputStr));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON);
        refreshViewModels(inputJSON);
        mHandler.postDelayed(this, 3000);       
    }
}
  • 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-10T09:57:58+00:00Added an answer on June 10, 2026 at 9:57 am

    You should probably use a Timer for this kind of short recurrent tasks:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        TimerTask task = new RepeatingTask();
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task, 0, 3000);
    }
    
    public class RepeatingTask extends TimerTask {
    
     private int len = 0; 
    
     private byte[] input = new byte[len];
    
    
     public RepeatingTask() {
         //try {
            Log.e(LOG_TAG, "Before inputJSON String");
            //inputJSON = dataInputStream.readUTF();
            //URL url = new URL("tcp://23.23.175.213:1337");
            //inputJSON = dataInputStream.readUTF();
            //inputstrrd = new InputStreamReader(socket.getInputStream());
            String hello = "hello world";
            //String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream()));
    
            //Convert 
            Log.e(LOG_TAG, "After inputJSON String:" + inputJSON); 
         /*}
         catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }*/
    
         //LOOK HERE FIRST  
         //inputJSON is what is received back from the server - Take the inputJSON 
         //String and use regular expressions HERE to remove all the other characters in the 
         //string except the payload JSON.
         //refreshViewModels(inputJSON);
     }
    
     @Override
     public void run() { 
          try {
              Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON");
              //outputstrwr.write(outputJSONserv);  //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
              //inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
              inputJSON = ConvertByteArrayToString(getFileBytes(inputStr));
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
    
          Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON);
          refreshViewModels(inputJSON);
     }
    

    }

    In your code, you mix the Thread pattern (started with the start() method) and handlers, which is a bit confusing.

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

Sidebar

Related Questions

I am using the thread.Abort method to kill the thread, but it not working.
As I understand Working with a DataContext in Parallel is not thread-safe but only
I've inherited code where BeginInvoke is called from the main thread (not a background
The following class is not thread-safe (as proven in Proving the following code not
do you know why the DragDrop event in F# is not working properly in
I'm having some trouble getting cross-thread communication/field-updating working properly in my VB.NET 2010 program.
Possible Duplicate: Cross-thread operation not valid I am trying to close the base of
Possible Duplicate: Cross-thread operation not valid: Control accessed from a thread other than the
I know UIView are not thread safe so i cant add a view on
So I know NSManagedObjects are not thread safe, and the best way to get

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.