2012-08-13 4 views
1

이것은 첫 번째 게시물이므로 필요한 경우 게시물에 추가해야 할 것이 있으면 알려주십시오.Java * 멀티 스레딩 with MySQL

나는 내 MySQL 데이터베이스에서 URL을 가져 와서 내 바탕 화면에 이미지를 다운로드 할 클래스로 보내는 프로그램을 가지고있다.

필자는 80,000 개의 이미지가 있어야하는데, 나는 그것을 빠르게 만들기 위해 멀티 스레딩을 활용하고 싶습니다.

나는 어딘가에 있었지만 지금은 붙어있어 멀티 스레딩을 처음 접했을 때 연구에 시간을 투자했습니다.

문제는 프로그램이 실행될 때 처음 5 개의 항목을 반복적으로 처리하는 루프를 통과하는 것입니다. 이 경우는 ExecutorFixedPoolSize가됩니다.

누군가 나를 도와 줄 수 있습니까?

내 코드는 다음과 같습니다.

public static void main(String[] args) { 

     try{ 

      countAllAltPicsNotSaved(); 
      System.out.println("Not saved: " + totalAltPicsNotSaved); 

      Class.forName("com.mysql.jdbc.Driver"); 
      Connection conn =DriverManager.getConnection(dbUrl + dbName, userName, password); 
      Statement s = conn.createStatement(); 
      s.executeQuery ("SELECT DISTINCT id from productsaltimages WHERE saved != 1"); 
      ResultSet rs = s.getResultSet(); 
      int saveCounter = 0, altImageCount = 0; 

      List<Thread> threads = new ArrayList<Thread>(); 



      while (rs.next()) { //Get ID 
       altImageCount = 0; //Product Alt Image Counter 
       String id = rs.getString("id"); //Product Table ID 

       Statement news = conn.createStatement(); //New Conn for get Alt Images From ID 
       news.executeQuery ("SELECT url from productsaltimages WHERE id ='"+id+"' AND saved != 5"); //Gets query from mySQL 

       ResultSet newrs = news.getResultSet(); //Resultset for AltImges for ID 

       ExecutorService executor = Executors.newFixedThreadPool(5); 
       Runnable task = null; 

       while (newrs.next()){ //Get Images 
        altImageCount++; //Increment ID Alt Image Counter 
        String url = newrs.getString("url"); 
        task = new DownloadImage(url, id, altImageCount); 
        executor.execute(task); 

       } 
      } 

     } catch (Exception e){ 
     } 

    } 

    public static void countAllAltPicsNotSaved() throws Exception { 
     try{ 
      totalAltPicsNotSaved=0; 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection conn =DriverManager.getConnection(dbUrl + dbName, userName, password); 
      Statement s = conn.createStatement(); 
      boolean sqlExecuteok = false; 
      s.executeQuery ("SELECT * from productsaltimages WHERE saved = '0'"); 
      ResultSet rs = s.getResultSet(); 
      while (rs.next()) { 
       totalAltPicsNotSaved++; 
      } 
      rs.close(); 
      s.close(); 
      conn.close(); 
     } catch (Exception e){ 
     } 
    } 


For my DownloadImage class code is: 

    public class DownloadImage implements Runnable 
{ 
    String url, id, threadName; 
    private int altImageCount = 0; 
    private static String userName = "owner"; 
    private static String password = "hello123"; 
    private static String dbUrl = "jdbc:mysql://localhost/"; 
    private static String dbName = "shop"; 
    private static String dbClass = "com.mysql.jdbc.Driver"; 

    public DownloadImage(String url, String id, int altImageCount) 
    { 
     this.url = url; 
     this.id = id; 
     this.altImageCount = altImageCount; 
    } 
    public void run() 
    { 
     while(true) 
     { 

     File file=new File("D:\\FBShop_AltImages\\" + id + "\\"); 
     boolean exists = file.exists(); 
     if (!exists) { 
      // Create multiple directories 
      boolean success = (new File("D:\\FBShop_AltImages\\" + id + "\\")).mkdirs(); 
     } 

     String newFilename = "D:\\FBShop_AltImages\\" + id + "\\" + id + "_" + altImageCount + ".jpg"; 

     try { 

      try { 
       BufferedImage image = null; 
       URL imgurl = new URL(url); 
       URLConnection con = imgurl.openConnection(); 
       con.setConnectTimeout(50 * 10000); 
       con.setReadTimeout(50 * 10000); 
       InputStream in = con.getInputStream(); 
       image = ImageIO.read(in); 
       ImageIO.write(image, "jpg", new File(newFilename)); 

      } catch (Exception e) { 
       System.out.println("Error getting image: " + url); 
      } 

      try { 
       //UpdateTable 
       Class.forName("com.mysql.jdbc.Driver"); 
       String updatesql = "UPDATE productsaltimages SET saved = '1' WHERE id = '"+id+"'"; 

       Connection conn =DriverManager.getConnection(dbUrl + dbName, userName, password); 
       Statement ups = conn.createStatement(); 
       int val = ups.executeUpdate(updatesql); 
       System.out.println("Task Complete: " + url); 
       try { 
        Thread.sleep(5000); 
       } catch (Exception e) { 
       } 
      } catch (Exception e) { 
      } 
     } catch (Exception e) { 
     } 
     //System.out.println("Thread Finished: " + threadName); 
     } 
    } 
} 
+0

@Eugene Lol, 미묘한 힌트 : p – Luc

답변

2
while(true){ 
    //lots of code inside you Runnable 

    Thread.sleep(5000); 
    //After the Thread will sleep it will restart it's work again, and again.. 
} 

때이를 것? 사실을 제거하면 당신은 잘되어야합니다.

+0

미안, 나는 왜 그런 일이 있었는지, 왜 거기에 있었는지 ... 메신저를 배우기 때문에 나는 그물에서 코드를 복사 해 넣었다. 나는 내가 할 수업을 배웠을 것 같다. 그!! 나는 그것을 제거하고 모든 것이 잘되었다 .. 몇 시간 걸렸지 만, 적어도 멀티 스레딩에 대해 연구하고 많은 것을 배웠다. 하하. 이것이 나를 위해 고쳐준 것이 고맙다. –

+0

@JeffreyHolmes 스레딩에 대해 배우고 싶다면 책을 읽어야합니다 - 동시성 동시성 - 가장 좋은 방법입니다. 건배! – Eugene

+1

멋진 사람 감사합니다 : D 난 그냥 할 수도있어! 나는이 웹 사이트를 그런 식으로 사랑한다 ... !!! !!! –