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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:13:00+00:00 2026-05-20T10:13:00+00:00

I have a thread that keeps looping and performs operations on an object. The

  • 0

I have a thread that keeps looping and performs operations on an object. The operations are wrapped in synchronized blocks. When I try to get a lock on the object from another thread (be it the UI or a new Thread), I keep waiting forever for the object to be released by the looping thread.

What am I missing here?

Activity

package com.ThreadTest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ThreadTest
    extends Activity
    implements OnClickListener
{
    private Looper looper;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((Button) findViewById(R.id.btnNewThread)).setOnClickListener(this);
        ((Button) findViewById(R.id.btnUIThread)).setOnClickListener(this);

        looper = new Looper();
        looper.startThread();
    }


    public void onClick(View view)
    {
        if (view.getId() == R.id.btnNewThread)
        {
            new Thread()
            {
                public void run()
                {
                    looper.sendRequest();
                }
            }.start();
        }
        else
        {
            looper.sendRequest();
        }
    }


    @Override
    protected void onPause()
    {
        looper.stopThread();
        super.onPause();
    }
}

The Constantly Looping Thread

package com.ThreadTest;

import android.util.Log;

public class Looper
{
    private static String TAG = "Looper";
    private final byte[] _data = new byte[65536];
    private final long sleepTime = 100;
    private final long sleepTime2 = 150;

    private LoopingThread _loopingThread;


    public Looper()
    {
    }


    public void stopThread()
    {
        Log.w(TAG, "stopThread");
        _loopingThread.shutdown();
    }


    public void startThread()
    {
        Log.w(TAG, "startThread");
        _loopingThread = new LoopingThread();
        _loopingThread.start();
    }


    public final void sendRequest()
    {
        final long preSync = System.currentTimeMillis();
        Log.w("sendRequest", "WAITING " +
            Thread.currentThread().getName() +
            " (" +
            Thread.currentThread().getPriority() +
            ")");

        synchronized (_data)
        {
            Log.e("sendRequest", "GOT LOCK " + (System.currentTimeMillis() - preSync) + "ms");

            try
            {
                Thread.sleep(sleepTime); //simulate processing
            }
            catch (final InterruptedException e)
            {
                //Ignore
            }
        }
    }

    private class LoopingThread
        extends Thread
    {
        private volatile boolean shutdown = false;


        public LoopingThread()
        {
            super("LoopingThread");
        }


        public void run()
        {
            long current = System.currentTimeMillis();

            while (!shutdown)
            {
                Log.i("LoopingThread", Thread.currentThread().getName() +
                    " (" +
                    Thread.currentThread().getPriority() +
                    ")");
                synchronized (_data)
                {
                    try
                    {
                        Thread.sleep(sleepTime);//simulate processing
                    }
                    catch (final Exception e)
                    {
                        //Ignore
                    }
                }
                current += 250;
                do
                {
                    try
                    {
                        synchronized (_data)
                        {
                            Thread.sleep(sleepTime2);//simulate processing
                        }
                    }
                    catch (final Exception e)
                    {
                        //Ignore
                        return;
                    }
                }
                while (!shutdown && current >= System.currentTimeMillis()); //loop for 250 ms
            }
        }


        public void shutdown()
        {
            shutdown = true;

            if (isAlive())
            {
                interrupt();

                try
                {
                    join();
                }
                catch (final InterruptedException e)
                {
                    // Ignored...
                }
            }
        }
    }

}

SAMPLE OUTPUT

...
03-04 16:03:27.675: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:28.056: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:28.355: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:28.717: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:29.040: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:29.400: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:29.721: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:30.034: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:30.125: WARN/sendRequest(18846): WAITING Thread-9 (5)
03-04 16:03:30.351: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:30.664: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:30.924: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:31.225: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:31.588: INFO/LoopingThread(18846): LoopingThread (5)
03-04 16:03:31.909: INFO/LoopingThread(18846): LoopingThread (5)
...(repeats)
  • 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-20T10:13:01+00:00Added an answer on May 20, 2026 at 10:13 am

    The sleep was put in the stripped down code to simulate processing. After some digging, the solution was to put a Thread.yield() after the do .. while loop so that the Looper thread breaks out of the busy loop long enough for the other thread to acquire the lock. The Looper thread was locking the object too fast and never giving the other thread a chance to access it.

    I guess the Android’s VM is different from Sun’s VM. This code executes fine in a Java applet and on the BlackBerry. However since the original code is obviously not the best way to perform the task at hand it will need to be modified. For now the Thread.yeild() is a band-aid solution.

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

Sidebar

Related Questions

I have a thread that downloads some images from internet using different proxies. Sometimes
I'm looking to have a class (subclassed from threading.Thread) that is initialised and started
I have a singleton that has a running thread for obtaining records from a
I have one thread that writes results into a Queue. In another thread (GUI),
I have a thread that, when its function exits its loop (the exit is
I have a thread that needs to be executed every 10 seconds. This thread
I have a thread that appends rows to self.output and a loop that runs
I have a thread that does the following: 1) Do some work 2) Wait
I have one thread that is receiving data over a socket like this: while
I currently have a thread that I created using CreateRemoteThread(). Everything works great. Upon

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.