Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:45:42+00:00 2026-06-09T17:45:42+00:00

This is my first post so please let me know if there is anything

  • 0

This is my first post so please let me know if there is anything I should add to the post if needed.

I have a program that grabs urls from my MySQL database and sends them to a class that will download the images to my desktop.

I have 80,000 images I need to get, and I well I want to utilize multithreading to make it quicker.

I have gotten somewhere but I am now stuck and have spent hours researching as I am brand new to multithreading.

The problem is when the program runs, it goes through a loop processing the first 5 entries over and over again. This happens to be the ExecutorFixedPoolSize.

Can someone help me please.

My Code is as follows.

    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);
      }
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:45:43+00:00Added an answer on June 9, 2026 at 5:45 pm
     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..
     }
    

    When will this end? Remove the while true and you should be good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, it is long post so if you need clarification please let me know.
This is my first post on stackoverflow, so please excuse me if my question
This is my first post on Stack, so please bear with me if I
This is my first post in this forum, so please, be patient with me.
This is my first post and I my first experience with jquery. I have
This is my first post here. I have a problem. I need to take
For Starters, i would like to note, that this is my first post on
This is my first post in any forum so please bear with me. I
This is my first post so if I do anything or phrase anything wrong
It's my very first post on this forum. I have stuck with the Microsoft

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.