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)
The
sleepwas put in the stripped down code to simulate processing. After some digging, the solution was to put aThread.yield()after thedo .. whileloop 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.