java多线程应用程序更新数据库
我有一个数据库,有 100 个条目需要更新某些表列条目 这是动态需要更新的。
基本上他们从磁盘收集数据的所有 100 个条目 并更新数据库表。为了获取数据库/磁盘信息,他们 必须获得锁,并尝试直到在 while 循环中获得锁。 一旦他们获得锁,就只能将最新的磁盘信息更新到数据库。
我有一个以下伪代码,它基本上按顺序执行上述工作。 我想以多线程方式运行它们,以便可以完成并行工作。 你能指导我吗?我对java多线程程序完全陌生。
预先感谢您的帮助。
while(true)
{
for(int i=0,i<100;i++)
{
//Get the info from Disk
String diskInfo=getDiskInfo(i);
//Get the info from DB table
String dbInfo=getDBInfo(i);
if (! diskInfo.equals(dbInfo))
{
//Update DB with diskInfo
boolean status=UpdateDB(i);
}
}
sleep(2000);
}
//Get the info from Disk
public String getDiskInfo()
{
//Get the disk
//lock the disk wait if busy
while(true)
{
//get disk lock
sleep(2000);
}
//fetch data
String data = "test";
//unlock disk
return data;
}
public String getDBInfo()
{
//Get the DB
//lock the DB wait if busy
while(true)
{
//get DB lock
sleep(2000);
}
//fetch data
//select data from X;
String data = "test";
//unlock disk
return data;
}
public boolean UpdateDB()
{
//Get the DB
//lock the DB wait if busy
while(true)
{
//get DB lock
sleep(2000);
}
//fetch data
if(!getDiskInfo(),equals(getDBInfo())
{
//lock the DB wait if busy
while(true)
{
//get DB lock
sleep(2000);
}
status=UpdateDB();
}
else
{
//no update needed
status=false;
}
return status;
}
I have a DB which has 100 entries need to update certain table colum entry
which is dynamically requires update.
Essentially all the 100 entries they collect the data from disk
and update the DB tables.In order to get the db/disk info they
have to get the lock which tries till gets the lock in the while loop.
Once they get the lock then only can update the latest diskinfo to the DB.
I have a following pesudo code which essentially does the above said work sequentially.
I want to run them multithreaded way so that parallel work can be done.
Could you please guide me.I am completely new to the java multithread program.
Thanksin advance for your help.
while(true)
{
for(int i=0,i<100;i++)
{
//Get the info from Disk
String diskInfo=getDiskInfo(i);
//Get the info from DB table
String dbInfo=getDBInfo(i);
if (! diskInfo.equals(dbInfo))
{
//Update DB with diskInfo
boolean status=UpdateDB(i);
}
}
sleep(2000);
}
//Get the info from Disk
public String getDiskInfo()
{
//Get the disk
//lock the disk wait if busy
while(true)
{
//get disk lock
sleep(2000);
}
//fetch data
String data = "test";
//unlock disk
return data;
}
public String getDBInfo()
{
//Get the DB
//lock the DB wait if busy
while(true)
{
//get DB lock
sleep(2000);
}
//fetch data
//select data from X;
String data = "test";
//unlock disk
return data;
}
public boolean UpdateDB()
{
//Get the DB
//lock the DB wait if busy
while(true)
{
//get DB lock
sleep(2000);
}
//fetch data
if(!getDiskInfo(),equals(getDBInfo())
{
//lock the DB wait if busy
while(true)
{
//get DB lock
sleep(2000);
}
status=UpdateDB();
}
else
{
//no update needed
status=false;
}
return status;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会编写代码来执行此操作。我会同步 Java 对象或使用数据库设施进行隔离来完成您想要的操作。
I would not write code to do this. I'd synchronize the Java objects or use the database facilities for isolation to do what you want.
据我所知,您只需要一个循环(最外层循环) 多线程程序的工作方式与单线程程序类似,因为到处都有无限循环并不是一个好主意。
我将有一个检查线程,它将任务添加到第一个循环中的线程池中,并删除其余的任务。
As far as I can see you only want one loop (the outer most loop) Multi-threaded programs work similar to single threaded programs in that its not a good idea to have infinite loops all over the place.
I would have one checking thread which adds tasks to a thread pool in your first loop and remove the rest.
api java.util.concurrent。
多线程很难。
sleep() 调用和无限循环最好用于填充,因为如果您尝试使用多线程,您会发现令人讨厌的惊喜。
也就是说,您的代码应该相对容易转换为多线程代码。
api java.util.concurrent.
Multithreading is hard.
That sleep() call and inifinite loops better be for filling because you are going to find nasty surprises if you try that with multithreading.
That said your code should be relatively easy to transform to a multithreading one.