📄 threadpool.java
字号:
// Developed by Kinva Network Inc. 2000
// Source File Name: ThreadPool.java
package com.kinva.util.thread;
import java.io.PrintStream;
// Referenced classes of package com.kinva.util.thread:
// DefaultPoolThread, ThreadPoolEntry, PoolThread
public class ThreadPool
{
public ThreadPool()
{
killed = false;
}
public synchronized void addThread(PoolThread poolthread)
{
if(!killed)
{
ThreadPoolEntry threadpoolentry = new ThreadPoolEntry(this, poolthread);
insertIdle(threadpoolentry);
threadpoolentry.init();
}
}
public void addThread(PoolThread apoolthread[])
{
for(int i = apoolthread.length; --i >= 0;)
addThread(apoolthread[i]);
}
public void addThread(Runnable runnable)
{
addThread(((PoolThread) (new DefaultPoolThread(runnable))));
}
public synchronized ThreadPoolEntry getEntry(boolean flag)
{
ThreadPoolEntry threadpoolentry;
for(threadpoolentry = idleList; threadpoolentry == null;)
if(flag)
{
acquire();
threadpoolentry = idleList;
} else
{
return null;
}
idleList = threadpoolentry.next;
if(idleList != null)
idleList.prev = null;
threadpoolentry.next = null;
threadpoolentry.prev = null;
insertBusy(threadpoolentry);
return threadpoolentry;
}
public synchronized ThreadPoolEntry getEntry()
{
return getEntry(false);
}
public PoolThread getThread(boolean flag)
{
ThreadPoolEntry threadpoolentry = getEntry(flag);
if(threadpoolentry != null)
return threadpoolentry.getThread();
else
return null;
}
public PoolThread getThread()
{
return getThread(false);
}
protected synchronized void acquire()
{
while(idleList == null)
try
{
wait();
}
catch(InterruptedException interruptedexception)
{
interruptedexception.printStackTrace();
}
}
synchronized void insertIdle(ThreadPoolEntry threadpoolentry)
{
if(busyList == threadpoolentry)
busyList = busyList.next;
if(threadpoolentry.next != null)
threadpoolentry.next.prev = threadpoolentry.prev;
if(threadpoolentry.prev != null)
threadpoolentry.prev.next = threadpoolentry.next;
threadpoolentry.next = null;
threadpoolentry.prev = null;
if(reClaim(threadpoolentry) || killed)
{
System.out.println("Reclaiming " + threadpoolentry.getThread());
threadpoolentry.shutdown();
return;
}
if(idleList == null)
{
idleList = threadpoolentry;
} else
{
threadpoolentry.next = idleList;
idleList.prev = threadpoolentry;
idleList = threadpoolentry;
}
notifyAll();
}
protected boolean reClaim(ThreadPoolEntry threadpoolentry)
{
return false;
}
private void insertBusy(ThreadPoolEntry threadpoolentry)
{
if(busyList == null)
{
busyList = threadpoolentry;
return;
} else
{
threadpoolentry.next = busyList;
busyList.prev = threadpoolentry;
busyList = threadpoolentry;
return;
}
}
public synchronized void shutdown(boolean flag)
{
killed = true;
for(; idleList != null; idleList = idleList.next)
idleList.shutdown();
if(!flag)
return;
for(; busyList != null; busyList = busyList.next)
busyList.shutdown();
}
public synchronized int getBusyListCount()
{
ThreadPoolEntry threadpoolentry = busyList;
int i;
for(i = 0; threadpoolentry != null; i++)
threadpoolentry = threadpoolentry.next;
return i;
}
private ThreadPoolEntry idleList;
private ThreadPoolEntry busyList;
private boolean killed;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -