public class BatchPersistent
{
private static Logger logger = Logger.getLogger(BatchPersistent.class);
private static int count = 0;
// SQL 数组的初始化大小
private static final int INITSIZE = 500;
private static TableName_Field[] sqlList = new TableName_Field[INITSIZE];
// 用于同步写入队列
private static Object object = new Object();
// 用于同步唤醒批量处理线程
private static Object waitobject = new Object();
private static Thread thread = null;
private static final String batchThreadName = "BatchThread";
static
{
thread = new BatchThread(batchThreadName);
thread.start();
logger.info("Batch Persistent Thread is starting!");
Runtime.getRuntime().addShutdownHook(new StopHookThread());
logger.info("Register shutdown hook is sucessfuly!");
}
/**
* 批量写数据库的线程
*
* 2012-1-10
*/
public static class BatchThread extends Thread
{
public BatchThread(String batchThread)
{
this.setName(batchThread);
}
public void run()
{
while (true)
{
// 取批量的SQL
TableName_Field[] tnfs = getTNFFromlList();
// 如果取不到,让线程等待到指定的时间醒过来自动扫描,或者让其他的线程唤醒
if (tnfs == null || tnfs.length == 0)
{
try
{
synchronized (waitobject)
{
logger.info("The size of SQLs is zero,Batch Thread waitting ("
+ Tools.getTHREADSLEEP() + "MS)");
waitobject.wait(Tools.getTHREADSLEEP());
logger.info("Batch Thread is wake up");
}
}
catch (Exception e)
{
logger.error("Have problem while thread waitting", e);
}
}
else
{
// 批量执行SQL语句
batchInsertToDB(tnfs);
// 为了减少数据性能消耗,如果第一次取的数据小于maxeachBatch,
// 还是让Batch Thread继续等待
if (tnfs.length < Tools.getMaxeachBatch())
{
try
{
logger.info("The size of SQLs less than "
+ "maxeachBatch,Batch Thread waitting ("
+ Tools.getTHREADSLEEP() + "MS)");
waitobject.wait(Tools.getTHREADSLEEP());
logger.info("Batch Thread is wake up");
}
catch (Exception e)
{
logger.error("Have problem while thread waitting",
e);
}
}
}
}
}
}
{
private static Logger logger = Logger.getLogger(BatchPersistent.class);
private static int count = 0;
// SQL 数组的初始化大小
private static final int INITSIZE = 500;
private static TableName_Field[] sqlList = new TableName_Field[INITSIZE];
// 用于同步写入队列
private static Object object = new Object();
// 用于同步唤醒批量处理线程
private static Object waitobject = new Object();
private static Thread thread = null;
private static final String batchThreadName = "BatchThread";
static
{
thread = new BatchThread(batchThreadName);
thread.start();
logger.info("Batch Persistent Thread is starting!");
Runtime.getRuntime().addShutdownHook(new StopHookThread());
logger.info("Register shutdown hook is sucessfuly!");
}
/**
* 批量写数据库的线程
*
* 2012-1-10
*/
public static class BatchThread extends Thread
{
public BatchThread(String batchThread)
{
this.setName(batchThread);
}
public void run()
{
while (true)
{
// 取批量的SQL
TableName_Field[] tnfs = getTNFFromlList();
// 如果取不到,让线程等待到指定的时间醒过来自动扫描,或者让其他的线程唤醒
if (tnfs == null || tnfs.length == 0)
{
try
{
synchronized (waitobject)
{
logger.info("The size of SQLs is zero,Batch Thread waitting ("
+ Tools.getTHREADSLEEP() + "MS)");
waitobject.wait(Tools.getTHREADSLEEP());
logger.info("Batch Thread is wake up");
}
}
catch (Exception e)
{
logger.error("Have problem while thread waitting", e);
}
}
else
{
// 批量执行SQL语句
batchInsertToDB(tnfs);
// 为了减少数据性能消耗,如果第一次取的数据小于maxeachBatch,
// 还是让Batch Thread继续等待
if (tnfs.length < Tools.getMaxeachBatch())
{
try
{
logger.info("The size of SQLs less than "
+ "maxeachBatch,Batch Thread waitting ("
+ Tools.getTHREADSLEEP() + "MS)");
waitobject.wait(Tools.getTHREADSLEEP());
logger.info("Batch Thread is wake up");
}
catch (Exception e)
{
logger.error("Have problem while thread waitting",
e);
}
}
}
}
}
}