1 Reply Latest reply on Nov 3, 2008 8:55 PM by 807589

    Thread notify and wait

    807589
      My project is to write a java thread to read all tables from a database and write it into another database.
      And i'd almost complete the coding (assuming that), but there's a problem regarding the thread. The thread is like stopping in the half way... and i'm not sure is it waiting or how...

      The system will call 2 thread together. Read and write thread. Here's the coding of my project:
      import java.sql.*;
      public class DBThread{
           private String statement, str;
           private DBConnection mysql, access;
           private Statement stm;
           public DBThread(){
                statement = null;
           }
           
           public synchronized void readTable(DBConnection mysql, String table) throws InterruptedException{
                
                try{
                     System.out.println("\nPreparing SQL Statement...");
                     stm                  = mysql.connection.createStatement();
               ResultSet rs         = stm.executeQuery("SELECT * FROM " + table);
                     ResultSetMetaData md = rs.getMetaData();
                     String text, text2;
                     
                     //Prepare Statement Header
                     int columns=md.getColumnCount();
                     text = "INSERT INTO " + table + "(";
                 
                     for(int i=1;i<=columns;++i){
                          text += "`" + md.getColumnName(i) + "`,";
                     }
                     text = text.substring(0,text.length()-1);
                     text += ") VALUES ";
                     
                     while(rs.next()){
                          System.out.println("in read "+statement);
                          while(statement != null)
                               wait();
                          System.out.println("statement is null");
                          text2 = "(";
                          for(int i=1;i<=columns;++i) {
                               text2 += "'" + rs.getString(i) + "',";
                          }
                          text2 = text2.substring(0,text2.length()-1);
                          text2 += ");";
                          
                          statement = text + text2;
                          System.out.println("statement not null anymore");
                          str = "1";
                          notifyAll();
                          while(str != null){
                               wait();
                          }System.out.println("finished waiting");
                          statement = null;
                          notifyAll();
                          
               }
                }catch(Exception e){e.printStackTrace();}     
           }
           
           public synchronized void writeTable(DBConnection access) throws InterruptedException{
                System.out.println("in write "+statement);
                
                
                try{
                     while(statement == null)
                          wait();
                     System.out.println("\nPrepare to write...\n");
                     access.executeQuery(statement);
                     System.out.println("Written: " + statement);
                     statement = null;
                     str = null;
                     notifyAll();
                }catch(Exception e){e.printStackTrace();}
                System.out.println("in write "+statement);
                
           }
      }
      and the output i got is:
      in write null
      
      Reading: activities
      
      Preparing SQL Statement...
      in read null
      statement is null
      statement not null anymore
      
      Prepare to write...
      
      Written: INSERT INTO activities(`activity_id`,`title`,`details`,`date_start`,`date_end`,`time`,`add_notes`,`create_date`,`update_date`) VALUES ('1','Gotong Royong','Clean up the rubbish','2008-07-28','2008-07-28','0900','All students of block A','2008-06-01 00:00:00','2008-06-01 00:00:00');
      in write null
      finished waiting
      in read null
      statement is null
      statement not null anymore
      It doesn't continue to write, and i can't find out the reason.
      Thanks a lot for the help!