package org.snmp4j.util;

import java.util.List;
import java.util.Vector;
import org.snmp4j.SNMP4JSettings;

/* JADX INFO: loaded from: classes.dex */
public class ThreadPool implements WorkerPool {
    protected Vector<TaskManager> taskManagers;
    protected String name = "ThreadPool";
    protected volatile boolean stop = false;
    protected boolean respawnThreads = false;

    class TaskManager extends Thread {
        private volatile boolean run;
        private WorkerTask task;

        public TaskManager(String str) {
            super(str);
            this.task = null;
            this.run = true;
        }

        public synchronized void execute(WorkerTask workerTask) {
            if (this.task != null) {
                throw new IllegalStateException("TaskManager is not idle");
            }
            this.task = workerTask;
            notify();
        }

        public boolean isIdle() {
            return this.task == null && this.run;
        }

        public boolean isStopped() {
            return ThreadPool.this.stop;
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public synchronized void run() {
            while (!ThreadPool.this.stop && this.run) {
                if (this.task != null) {
                    this.task.run();
                    synchronized (ThreadPool.this) {
                        this.task = null;
                        ThreadPool.this.notify();
                    }
                } else {
                    try {
                        wait();
                    } catch (InterruptedException unused) {
                        this.run = ThreadPool.this.respawnThreads;
                    }
                }
            }
        }

        public void terminate() {
            ThreadPool.this.stop = true;
            WorkerTask workerTask = this.task;
            if (workerTask != null) {
                workerTask.terminate();
            }
        }
    }

    protected ThreadPool() {
    }

    public static ThreadPool create(String str, int i2) {
        ThreadPool threadPool = new ThreadPool();
        threadPool.setup(str, i2);
        return threadPool;
    }

    @Override // org.snmp4j.util.WorkerPool
    public synchronized void cancel() {
        this.stop = true;
        for (int i2 = 0; i2 < this.taskManagers.size(); i2++) {
            TaskManager taskManager = this.taskManagers.get(i2);
            taskManager.terminate();
            taskManager.interrupt();
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:17:0x0035, code lost:
    
        wait();
     */
    @Override // org.snmp4j.util.WorkerPool
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public synchronized void execute(org.snmp4j.util.WorkerTask r4) {
        /*
            r3 = this;
            monitor-enter(r3)
        L1:
            r0 = 0
        L2:
            java.util.Vector<org.snmp4j.util.ThreadPool$TaskManager> r1 = r3.taskManagers     // Catch: java.lang.Throwable -> L3e
            int r1 = r1.size()     // Catch: java.lang.Throwable -> L3e
            if (r0 >= r1) goto L35
            java.util.Vector<org.snmp4j.util.ThreadPool$TaskManager> r1 = r3.taskManagers     // Catch: java.lang.Throwable -> L3e
            java.lang.Object r1 = r1.get(r0)     // Catch: java.lang.Throwable -> L3e
            org.snmp4j.util.ThreadPool$TaskManager r1 = (org.snmp4j.util.ThreadPool.TaskManager) r1     // Catch: java.lang.Throwable -> L3e
            boolean r2 = r3.respawnThreads     // Catch: java.lang.Throwable -> L3e
            if (r2 == 0) goto L27
            boolean r2 = r1.isAlive()     // Catch: java.lang.Throwable -> L3e
            if (r2 != 0) goto L27
            org.snmp4j.util.ThreadPool$TaskManager r1 = new org.snmp4j.util.ThreadPool$TaskManager     // Catch: java.lang.Throwable -> L3e
            java.lang.String r2 = r3.name     // Catch: java.lang.Throwable -> L3e
            java.lang.String r2 = r3.getTaskManagerName(r2, r0)     // Catch: java.lang.Throwable -> L3e
            r1.<init>(r2)     // Catch: java.lang.Throwable -> L3e
        L27:
            boolean r2 = r1.isIdle()     // Catch: java.lang.Throwable -> L3e
            if (r2 == 0) goto L32
            r1.execute(r4)     // Catch: java.lang.Throwable -> L3e
            monitor-exit(r3)
            return
        L32:
            int r0 = r0 + 1
            goto L2
        L35:
            r3.wait()     // Catch: java.lang.InterruptedException -> L39 java.lang.Throwable -> L3e
            goto L1
        L39:
            r0 = move-exception
            r3.handleInterruptedExceptionOnExecute(r0, r4)     // Catch: java.lang.Throwable -> L3e
            goto L1
        L3e:
            r4 = move-exception
            monitor-exit(r3)
            goto L42
        L41:
            throw r4
        L42:
            goto L41
        */
        throw new UnsupportedOperationException("Method not decompiled: org.snmp4j.util.ThreadPool.execute(org.snmp4j.util.WorkerTask):void");
    }

    public String getName() {
        return this.name;
    }

    protected String getTaskManagerName(String str, int i2) {
        return str + "." + i2;
    }

    protected void handleInterruptedExceptionOnExecute(InterruptedException interruptedException, WorkerTask workerTask) {
        if (SNMP4JSettings.isForwardRuntimeExceptions()) {
            throw new RuntimeException(interruptedException);
        }
    }

    public synchronized void interrupt() {
        for (int i2 = 0; i2 < this.taskManagers.size(); i2++) {
            this.taskManagers.get(i2).interrupt();
        }
    }

    @Override // org.snmp4j.util.WorkerPool
    public synchronized boolean isIdle() {
        for (int i2 = 0; i2 < this.taskManagers.size(); i2++) {
            if (!this.taskManagers.get(i2).isIdle()) {
                return false;
            }
        }
        return true;
    }

    public boolean isRespawnThreads() {
        return this.respawnThreads;
    }

    public void setRespawnThreads(boolean z) {
        this.respawnThreads = z;
    }

    protected void setup(String str, int i2) {
        this.name = str;
        this.taskManagers = new Vector<>(i2);
        for (int i3 = 0; i3 < i2; i3++) {
            TaskManager taskManager = new TaskManager(getTaskManagerName(str, i3));
            this.taskManagers.add(taskManager);
            taskManager.start();
        }
    }

    @Override // org.snmp4j.util.WorkerPool
    public void stop() {
        List list;
        synchronized (this) {
            this.stop = true;
            list = (List) this.taskManagers.clone();
        }
        for (int i2 = 0; i2 < list.size(); i2++) {
            TaskManager taskManager = (TaskManager) list.get(i2);
            taskManager.terminate();
            synchronized (taskManager) {
                taskManager.notify();
            }
            try {
                taskManager.join();
            } catch (InterruptedException unused) {
                Thread.currentThread().interrupt();
            }
        }
    }

    @Override // org.snmp4j.util.WorkerPool
    public synchronized boolean tryToExecute(WorkerTask workerTask) {
        for (int i2 = 0; i2 < this.taskManagers.size(); i2++) {
            TaskManager taskManager = this.taskManagers.get(i2);
            if (this.respawnThreads && !taskManager.isAlive()) {
                taskManager = new TaskManager(getTaskManagerName(this.name, i2));
            }
            if (taskManager.isIdle()) {
                taskManager.execute(workerTask);
                return true;
            }
        }
        return false;
    }
}
