📄 threadpool.java
字号:
{
return run(runnable, false, false);
}
public threadPoolWorker run(AERunnable runnable, boolean high_priority, boolean manualRelease)
{
threadPoolWorker recursive_worker;
ThreadPoolTask task;
if (manualRelease && !(runnable instanceof ThreadPoolTask))
throw new IllegalArgumentException("manual release only allowed for ThreadPoolTasks");
if (manualRelease)
((ThreadPoolTask)runnable).manualRelease = 1;
if (queue_when_full || thread_sem.reserveIfAvailable())
break MISSING_BLOCK_LABEL_153;
recursive_worker = (threadPoolWorker)tls.get();
if (recursive_worker == null || recursive_worker.getOwner() != this)
{
checkWarning();
thread_sem.reserve();
break MISSING_BLOCK_LABEL_153;
}
if (!(runnable instanceof ThreadPoolTask))
break MISSING_BLOCK_LABEL_145;
task = (ThreadPoolTask)runnable;
task.worker = recursive_worker;
task.taskStarted();
runIt(runnable);
task.join();
task.taskCompleted();
break MISSING_BLOCK_LABEL_150;
Exception exception;
exception;
task.taskCompleted();
throw exception;
runIt(runnable);
return recursive_worker;
threadPoolWorker allocated_worker;
synchronized (this)
{
if (high_priority)
task_queue.add(0, runnable);
else
task_queue.add(runnable);
if (queue_when_full && !thread_sem.reserveIfAvailable())
{
allocated_worker = null;
checkWarning();
} else
{
allocated_worker = new threadPoolWorker();
}
}
return allocated_worker;
}
protected void runIt(AERunnable runnable)
{
if (log_cpu)
{
long start_cpu = log_cpu ? Java15Utils.getThreadCPUTime() : 0L;
long start_time = SystemTime.getHighPrecisionCounter();
runnable.run();
if (start_cpu > 0L)
{
long end_cpu = log_cpu ? Java15Utils.getThreadCPUTime() : 0L;
long diff_cpu = (end_cpu - start_cpu) / 0xf4240L;
long end_time = SystemTime.getHighPrecisionCounter();
long diff_millis = (end_time - start_time) / 0xf4240L;
if (diff_cpu > 10L || diff_millis > 10L)
System.out.println((new StringBuilder()).append(TimeFormatter.milliStamp()).append(": Thread: ").append(Thread.currentThread().getName()).append(": ").append(runnable).append(" -> ").append(diff_cpu).append("/").append(diff_millis).toString());
}
} else
{
runnable.run();
}
}
protected void checkWarning()
{
if (warn_when_full)
{
String task_names = "";
try
{
synchronized (this)
{
for (int i = 0; i < busy.size(); i++)
{
threadPoolWorker x = (threadPoolWorker)busy.get(i);
AERunnable r = x.runnable;
if (x == null)
continue;
String name;
if (r instanceof ThreadPoolTask)
name = ((ThreadPoolTask)r).getName();
else
name = x.getClass().getName();
task_names = (new StringBuilder()).append(task_names).append(task_names.length() != 0 ? "," : "").append(name).toString();
}
}
}
catch (Throwable e) { }
Debug.out((new StringBuilder()).append("Thread pool '").append(getName()).append("' is full (busy=").append(task_names).append(")").toString());
warn_when_full = false;
}
}
public AERunnable[] getQueuedTasks()
{
ThreadPool threadpool = this;
JVM INSTR monitorenter ;
AERunnable res[];
res = new AERunnable[task_queue.size()];
task_queue.toArray(res);
return res;
Exception exception;
exception;
throw exception;
}
public int getQueueSize()
{
ThreadPool threadpool = this;
JVM INSTR monitorenter ;
return task_queue.size();
Exception exception;
exception;
throw exception;
}
public boolean isQueued(AERunnable task)
{
ThreadPool threadpool = this;
JVM INSTR monitorenter ;
return task_queue.contains(task);
Exception exception;
exception;
throw exception;
}
public AERunnable[] getRunningTasks()
{
List runnables = new ArrayList();
synchronized (this)
{
Iterator it = busy.iterator();
do
{
if (!it.hasNext())
break;
threadPoolWorker worker = (threadPoolWorker)it.next();
AERunnable runnable = worker.getRunnable();
if (runnable != null)
runnables.add(runnable);
} while (true);
}
AERunnable res[] = new AERunnable[runnables.size()];
runnables.toArray(res);
return res;
}
public int getRunningCount()
{
int res = 0;
synchronized (this)
{
Iterator it = busy.iterator();
do
{
if (!it.hasNext())
break;
threadPoolWorker worker = (threadPoolWorker)it.next();
AERunnable runnable = worker.getRunnable();
if (runnable != null)
res++;
} while (true);
}
return res;
}
public boolean isFull()
{
return thread_sem.getValue() == 0;
}
protected void checkTimeouts()
{
synchronized (this)
{
long diff = task_total - task_total_last;
task_average.addValue(diff);
task_total_last = task_total;
if (debug_thread_pool_log_on)
System.out.println((new StringBuilder()).append("ThreadPool '").append(getName()).append("'/").append(thread_name_index).append(": max=").append(max_size).append(",sem=[").append(thread_sem.getString()).append("],busy=").append(busy.size()).append(",queue=").append(task_queue.size()).toString());
long now = SystemTime.getCurrentTime();
for (int i = 0; i < busy.size(); i++)
{
threadPoolWorker x = (threadPoolWorker)busy.get(i);
long elapsed = now - x.run_start_time;
if (elapsed <= (long)(10000 * (x.warn_count + 1)))
continue;
x.warn_count++;
if (execution_limit <= 0L || elapsed <= execution_limit)
continue;
AERunnable r = x.runnable;
if (r == null)
continue;
try
{
if (r instanceof ThreadPoolTask)
((ThreadPoolTask)r).interruptTask();
else
x.interrupt();
}
catch (Throwable e)
{
DebugLight.printStackTrace(e);
}
}
}
}
public String getName()
{
return name;
}
void releaseManual(ThreadPoolTask toRelease)
{
if (!busy.contains(toRelease.worker) || toRelease.manualRelease != 2)
throw new IllegalStateException("task already released or not manually releasable");
synchronized (this)
{
long elapsed = SystemTime.getCurrentTime() - toRelease.worker.run_start_time;
if (elapsed <= 10000L);
busy.remove(toRelease.worker);
if (busy.size() == 0 && !debug_thread_pool)
synchronized (busy_pools)
{
busy_pools.remove(this);
}
if (busy.size() == 0)
thread_sem.release();
else
new threadPoolWorker();
}
}
public void registerThreadAsChild(threadPoolWorker parent)
{
if (tls.get() == null || tls.get() == parent)
tls.set(parent);
else
throw new IllegalStateException("another parent is already set for this thread");
}
public void deregisterThreadAsChild(threadPoolWorker parent)
{
if (tls.get() == parent)
tls.set(null);
else
throw new IllegalStateException("tls is not set to parent");
}
static
{
if (System.getProperty("transitory.startup", "0").equals("0"))
AEDiagnostics.addEvidenceGenerator(new AEDiagnosticsEvidenceGenerator() {
public void generate(IndentWriter writer)
{
writer.println("Thread Pools");
writer.indent();
List pools;
synchronized (ThreadPool.busy_pools)
{
pools = new ArrayList(ThreadPool.busy_pools);
}
for (int i = 0; i < pools.size(); i++)
((ThreadPool)pools.get(i)).generateEvidence(writer);
writer.exdent();
break MISSING_BLOCK_LABEL_87;
Exception exception1;
exception1;
writer.exdent();
throw exception1;
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -