java多线程应用程序更新数据库

发布于 2024-11-02 21:28:52 字数 1521 浏览 1 评论 0原文

我有一个数据库,有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-11-09 21:28:52

我不会编写代码来执行此操作。我会同步 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.

一页 2024-11-09 21:28:52

据我所知,您只需要一个循环(最外层循环) 多线程程序的工作方式与单线程程序类似,因为到处都有无限循环并不是一个好主意。

我将有一个检查线程,它将任务添加到第一个循环中的线程池中,并删除其余的任务。

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.

嗼ふ静 2024-11-09 21:28:52
  1. 了解多线程概念。
  2. 然后学习java多线程概念。
  3. 然后学习java多线程
    api java.util.concurrent。
  4. 然后使用java api。确保使用最合适的类。

多线程很难。

sleep() 调用和无限循环最好用于填充,因为如果您尝试使用多线程,您会发现令人讨厌的惊喜。

也就是说,您的代码应该相对容易转换为多线程代码。

  1. Learn multithreading concepts.
  2. Then learn java multithreading concepts.
  3. Then learn the java multithreading
    api java.util.concurrent.
  4. Then use the java api. Be sure to use the most appropriate classes.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文