//获得无限制线程
newCachedPool = Executors.newCachedThreadPool();
//执行主流程方法
run(){
//主流程是多线程
operator();
}//试图同步代码,在下面加锁
public void operator(){
Lock lock = new ReentrantLock();
lock.lock();
try{
//插入对象到数据库
Model model = this.insertDB();
//中间经历若干操作
fun1();
fun2();
..................
//分支流程是多线程
service.threadfunction(model);
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
//在此方法中创建新的线程,在线程中发送webservice,并根据回执(returnStr )更新上面insertDB()插入的数据,
//但是在高并发的情况下,有时会找不到insertDB()插入的值,可能此时未插入成功,但是新的ID(model.id)存在
//导致更新不成功,线程池修改成单线程不存在此问题
threadfunction(Model model){
//获得无限制线程
newCachedPool = Executors.newCachedThreadPool();
run(){
//将model转换成xml,发送webservice
String xml = model
String returnStr = send.webservice(model);
if(returnStr!=null){
//执行update方法,但是在高并发时,model会有少数几条未存储,所以更新方法无效,在所有线程执行完后,model
//发现已经存储了,但是update语句未执行
update Model a set a.falg="完成" where id = model.id
}
}
}
这是为什么呢?另外线程池修改为单线程newSinglePool = Executors.newSingleThreadExecutor();就不会出现问题
newCachedPool = Executors.newCachedThreadPool();
//执行主流程方法
run(){
//主流程是多线程
operator();
}//试图同步代码,在下面加锁
public void operator(){
Lock lock = new ReentrantLock();
lock.lock();
try{
//插入对象到数据库
Model model = this.insertDB();
//中间经历若干操作
fun1();
fun2();
..................
//分支流程是多线程
service.threadfunction(model);
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
//在此方法中创建新的线程,在线程中发送webservice,并根据回执(returnStr )更新上面insertDB()插入的数据,
//但是在高并发的情况下,有时会找不到insertDB()插入的值,可能此时未插入成功,但是新的ID(model.id)存在
//导致更新不成功,线程池修改成单线程不存在此问题
threadfunction(Model model){
//获得无限制线程
newCachedPool = Executors.newCachedThreadPool();
run(){
//将model转换成xml,发送webservice
String xml = model
String returnStr = send.webservice(model);
if(returnStr!=null){
//执行update方法,但是在高并发时,model会有少数几条未存储,所以更新方法无效,在所有线程执行完后,model
//发现已经存储了,但是update语句未执行
update Model a set a.falg="完成" where id = model.id
}
}
}
这是为什么呢?另外线程池修改为单线程newSinglePool = Executors.newSingleThreadExecutor();就不会出现问题
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货