android新对象锁,Android的线程和对象锁定

我有一个不断循环和对象上执行操作的线程。这些操作被封装在​​块中。当我尝试从另一个线程(无论是UI还是一个新的线程)获取对象的锁时,我一直等待对象被循环线程释放。Android的线程和对象锁定

我在这里错过了什么?

活动

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();

}

}

不断循环螺纹

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...

}

}

}

}

}

样本输出

...

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)

2011-03-04

tuxGurl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值