📄 mthreadpool.java
字号:
package net.jumperz.util;
import java.util.*;
public final class MThreadPool
{
private static int DEFAULT_THREAD_COUNT = 20;
private LinkedList waitingThreadList;
private LinkedList runningThreadList;
private LinkedList commandList;
private boolean stopped = false;
private int maxThreadCount = 0;
private MLogger logger = new MNullLogger();
//---------------------------------------------------------------
public MThreadPool( int threadCount )
{
init( threadCount );
}
//---------------------------------------------------------------
public MThreadPool()
{
init( DEFAULT_THREAD_COUNT );
}
//--------------------------------------------------------------------------------
public void setLogger( MLogger logger )
{
this.logger = logger;
}
//---------------------------------------------------------------
private void init( int threadCount )
{
waitingThreadList = new LinkedList();
runningThreadList = new LinkedList();
commandList = new LinkedList();
//create worker threads
for( int i = 0; i < threadCount; ++i )
{
waitingThreadList.add( new MWorkerThread( this ) );
}
}
//---------------------------------------------------------------
private synchronized void doIt()
{
if( commandList.size() > 0
&& waitingThreadList.size() > 0
)
{
MCommand command = ( MCommand )commandList.getFirst();
commandList.removeFirst();
MWorkerThread workerThread = ( MWorkerThread )waitingThreadList.getFirst();
waitingThreadList.removeFirst();
runningThreadList.addLast( workerThread );
workerThread.setCommand( command );
workerThread.resumeThread();
}
int count = runningThreadList.size();
if( count > maxThreadCount )
{
logger.log( "maxThreadCount::" + count );
maxThreadCount = count;
}
}
//---------------------------------------------------------------
public synchronized void addCommand( MCommand command )
{
commandList.addLast( command );
doIt();
}
//--------------------------------------------------------------------------------
public synchronized boolean forceCommand( MCommand command )
{
if( waitingThreadList.size() > 0 )
{
commandList.addFirst( command );
doIt();
return true;
}
else
{
return false;
}
}
//---------------------------------------------------------------
public synchronized void setThreadWait( MWorkerThread workerThread )
{
runningThreadList.remove( workerThread );
if( stopped )
{
workerThread.terminate();
workerThread.resumeThread();
}
else
{
waitingThreadList.addLast( workerThread );
doIt();
}
}
//---------------------------------------------------------------
public synchronized void stop()
{
if( stopped )
{
return;
}
stopped = true;
//stop waiting threads
while( !waitingThreadList.isEmpty() )
{
MWorkerThread workerThread = ( MWorkerThread )waitingThreadList.getFirst();
waitingThreadList.removeFirst();
workerThread.terminate();
workerThread.resumeThread();
}
//stop running threads
while( !runningThreadList.isEmpty() )
{
MWorkerThread workerThread = ( MWorkerThread )runningThreadList.getFirst();
runningThreadList.removeFirst();
workerThread.breakThread();
}
}
//---------------------------------------------------------------
public int getRunningThreadCount()
{
return runningThreadList.size();
}
//---------------------------------------------------------------
public int getWaitingThreadCount()
{
return waitingThreadList.size();
}
//---------------------------------------------------------------
public int getCommandCount()
{
return commandList.size();
}
//--------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -