开发者

java multi threaded application to update DB

开发者 https://www.devze.com 2023-02-28 01:55 出处:网络
I have a DB which has 100 entries need to update certain table colum entry which is dynamically requires update.

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;  

}


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.


  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号