线程状态监视器.我该如何调试?是什么原因造成的? [英] Thread Status Monitor. How do I debug this? What's causing it?

查看:34
本文介绍了线程状态监视器.我该如何调试?是什么原因造成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Android 上进行开发,但我不明白为什么我的一些线程会进入监控"状态.我读过它可能是因为同步"问题,但我不确定对象如何不释放它们的锁.

I'm developing on Android and I can't figure out why some of my threads are going into a "monitor" status. I've read it could be because of a "synchronized" issue but I'm not sure how an object wouldn't release their lock.

任何人都可以帮忙调试这个问题,或者你看到我做错了什么吗?是同步对象没有被释放的问题还是我的加载没有正确超时并锁定所有线程?

Can anyone help with how to debug this or do you see anything I'm doing wrong? Is it an issue of synchronized objects not being released or is my loading not timing out properly and locking all the threads?

这是我使用同步的方式.

Here's how I'm using synchronized.

private Bitmap getFromSyncCache(String url) {
    if (syncCache == null) return null;
    synchronized (syncCache) {
        if (syncCache.hasObject(url)) {
            return syncCache.get(url);
        } else {
            return null;
        }
    }
}

这里:

bitmapLoader.setOnCompleteListener(new BitmapLoader.OnCompleteListener() {
            @Override
            public void onComplete(Bitmap bitmap) {
                if (syncCache != null) {
                    synchronized (syncCache) {
                        syncCache.put(bitmapLoader.getLoadUrl(), bitmap);
                    }
                }
                if (asyncCache != null) addToAsyncCache(bitmapLoader.getLoadUrl(), bitmap);
                if (onCompleteListener != null) onCompleteListener.onComplete(bitmap);
            }
        });

这是我的缓存

public class MemoryCache<T> implements Cache<T>{

private HashMap<String, SoftReference<T>> cache;

public MemoryCache() {
    cache = new HashMap<String, SoftReference<T>>();
}

@Override
public T get(String id) {
    if(!cache.containsKey(id)) return null;
    SoftReference<T> ref = cache.get(id);
    return ref.get();
}

@Override
public void put(String id, T object) {
    cache.put(id, new SoftReference<T>(object));
}

@Override
public void clearCache() {
    cache.clear();
}

@Override
public boolean hasObject(String id) {
    return cache.containsKey(id);
}

这就是我从网络加载图像的方式:

and this is how I'm loading the image from the web:

private void threadedLoad(String url) {
    cancel();
    bytesLoaded = 0;
    bytesTotal = 0;
    try {
        state = State.DOWNLOADING;
        conn = (HttpURLConnection) new URL(url).openConnection();
        bytesTotal = conn.getContentLength();

        // if we don't have a total can't track the progress
        if (bytesTotal > 0 && onProgressListener != null) {
            // unused               
        } else {
            conn.connect();
            inStream = conn.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inStream);
            state = State.COMPLETE;
            if (state != State.CANCELED) {
                if (bitmap != null) {
                    msgSendComplete(bitmap);
                } else {
                    handleIOException(new IOException("Skia could not decode the bitmap and returned null. Url: " + loadUrl));
                }
            }
            try {
                inStream.close();
            } catch(Exception e) {

            }
        }
    } catch (IOException e) {
        handleIOException(e);
    }
}

推荐答案

检查是否确实是死锁的一种方法是使用 Android Studio 的调试器:查看线程,右键单击处于监控"状态的线程,然后单击暂停".调试器会将您带到代码中线程卡住的行.

A way to check if it's indeed a deadlock is to use Android Studio's debugger: view the threads, right click on the threads that are in the "MONITOR" state and then click "Suspend." The debugger will take you to the line in the code at which the thread is stuck.

当我调试死锁时,两个线程都在等待同步语句.

When I was debugging my deadlock, both threads turned out to be waiting on the synchronized statements.

这篇关于线程状态监视器.我该如何调试?是什么原因造成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆