批量入库 BatchPersistent 01

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);
}
}
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值