package cn.com.magnity.magnitycx.sdk; import android.os.Handler; import android.os.Message; import java.util.ArrayList; /* loaded from: classes.dex */ public class AsyncLoadImage { private static final int MSG = 305419896; private Handler[] handlers_; private String imageDir_; private volatile int maxTaskLen = Integer.MAX_VALUE; private Object receiver_; private volatile boolean running_; private volatile ArrayList taskList; private volatile ArrayList taskList_; private ImageLoaderThread[] threads_; private WaitCondition wcObj_; public interface DelegateExecute { Object onBackToDo(Object obj); void onExecuteFinished(Object obj); } public AsyncLoadImage(String imageDir, int threadNum, Object receiver) { this.receiver_ = receiver; this.imageDir_ = imageDir; this.threads_ = new ImageLoaderThread[threadNum]; ImageLoaderThread[] imageLoaderThreadArr = this.threads_; int length = imageLoaderThreadArr.length; int i = 0; int index = 0; while (i < length) { ImageLoaderThread imageLoaderThread = imageLoaderThreadArr[i]; ImageLoaderThread thread = new ImageLoaderThread(index); thread.start(); i++; index++; } this.wcObj_ = new WaitCondition(); this.taskList_ = new ArrayList<>(); for (Handler handler : this.handlers_) { new Handler() { // from class: cn.com.magnity.magnitycx.sdk.AsyncLoadImage.1 @Override // android.os.Handler public void handleMessage(Message msg) { switch (msg.what) { case AsyncLoadImage.MSG /* 305419896 */: if (AsyncLoadImage.this.receiver_ instanceof DelegateExecute) { ((DelegateExecute) AsyncLoadImage.this.receiver_).onExecuteFinished(msg.obj); break; } break; } super.handleMessage(msg); } }; } } public void add(String name) { synchronized (this.wcObj_.cond) { if (!this.taskList_.contains(name)) { if (this.taskList_.size() > this.maxTaskLen) { } if (!this.taskList_.contains(name)) { this.taskList_.add(name); this.wcObj_.notified = true; this.wcObj_.cond.notify(); } } } } public void setTaskQueueLen(int len) { synchronized (this.wcObj_.cond) { this.maxTaskLen = len; } } public void removeAll() { synchronized (this.wcObj_.cond) { this.taskList_.clear(); } } public void exit() { this.running_ = false; synchronized (this.wcObj_.cond) { this.wcObj_.notified = true; this.wcObj_.cond.notifyAll(); } try { for (ImageLoaderThread thread : this.threads_) { thread.join(); } } catch (InterruptedException e) { } } private class ImageLoaderThread extends Thread { private int index_; public ImageLoaderThread(int index) { this.index_ = index; } @Override // java.lang.Thread, java.lang.Runnable public void run() { while (AsyncLoadImage.this.running_) { synchronized (AsyncLoadImage.this.wcObj_.cond) { while (!AsyncLoadImage.this.wcObj_.notified) { try { AsyncLoadImage.this.wcObj_.cond.wait(); } catch (InterruptedException e) { } } AsyncLoadImage.this.wcObj_.notified = false; if (AsyncLoadImage.this.taskList_.size() > 0) { String name = (String) AsyncLoadImage.this.taskList_.get(0); AsyncLoadImage.this.taskList_.remove(0); if (AsyncLoadImage.this.running_) { if (AsyncLoadImage.this.receiver_ instanceof DelegateExecute) { Object object = ((DelegateExecute) AsyncLoadImage.this.receiver_).onBackToDo(name); Message msg = new Message(); msg.what = AsyncLoadImage.MSG; msg.obj = object; AsyncLoadImage.this.handlers_[this.index_].sendMessage(msg); } } else { return; } } } } } } }