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

  • SEARCH
  • Home
  • 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 6379831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:11:19+00:00 2026-05-25T02:11:19+00:00

I have written a multi-threaded Java application which reads a bunch of .jar files

  • 0

I have written a multi-threaded Java application which reads a bunch of .jar files from a directory. This application spawns multiple threads and each threads reads bunch of jar files. I’m having trouble identifying the stopping condition for this application. How can i identify that all the files have been read?

The following is a snippet function which gets called from the run() method for each thread.

    import java.io.*;
import java.util.Enumeration;
import java.util.jar.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipException;
import java.io.FilenameFilter;

public class ArchiveFileTest implements Runnable {
     private static boolean stopAll = false;
     private static int threadNumber = 0;
     private int myNumber = 0;

     public ArchiveFileTest () {
            myNumber = threadNumber;
            threadNumber++;
     }

     public static boolean setStopAll () {
            return setStopAll(true);
     }

     public static boolean setStopAll (boolean b) {
            stopAll = b;
            return stopAll;
     }

     public static String[] listFiles (File parentDir,final String ext1,final String ext2,final String ext3,final String ext4) {
            String allFiles[] = parentDir.list(new FilenameFilter() { 
                 public boolean accept(File pDir, String fName) {
                        if (fName.endsWith("."+ext1) || fName.endsWith("."+ext2) || fName.endsWith("."+ext3) || fName.endsWith("."+ext4)) return true;
                        else return false;
                 }
            });
            for (int i=0; i<allFiles.length; i++)
                 allFiles[i] = parentDir.getAbsolutePath() + File.separator +  allFiles[i];
            return allFiles;
     }

     public ZipFile getMyZipFile (File parentDir) {
            String fn[] = listFiles(parentDir, "jar", "zip", "war", "rar");
            int fileNum = myNumber % fn.length;
            ZipFile zFile = null;
            for (int i=0; i<fn.length; i++) {
                 String jFile = fn[(fileNum + i)%fn.length];
                 try {
                        zFile = new ZipFile(jFile);
                        break;
                 } catch  (IOException e) {
                     setStopAll();
                 }
            }
            return zFile;
     }

     public void doStuff() throws Exception {
            File dName = new File("/home/sqatest/chander/sample-files");
          final int N_TIMES = 15;
            final int N_FILES = 500;
          int counter = 0;
            int fCount  = 0;
            if (!dName.isDirectory() || !dName.exists()) {
                 System.err.println("The parent directory given should point to an existing directory...");
                 setStopAll();
                 return;
            }
          while (counter < N_TIMES) { 
                 ZipFile zipFile = getMyZipFile(dName);
                 if (zipFile == null) {
                        System.err.println("No zip file entry for the Thread-" + myNumber);
                        break;
                 }
                 try {
                        Enumeration <? extends ZipEntry> zipEntries = zipFile.entries(); 
                        fCount = 0;
                        ZipEntry ze = null;
                        while (zipEntries.hasMoreElements()) {
                             ze = zipEntries.nextElement();

                             if (ze.isDirectory()) continue; // if it is a directory go to next entry

                             InputStream is = zipFile.getInputStream(ze); 
                             fCount++;
                             int readCount = 0;
                             try {
                                    while(is.read((new byte[50])) != -1 && readCount != 200) readCount++;
                                    System.out.println("Successfully Read " + zipFile.toString());

                                    //is.close();
                             } catch (IOException e) {
                                    e.printStackTrace();
                             }
                             if (fCount == N_FILES) break;  // read maximum of N_FILES
                        }
                        if (stopAll) break;
                 } catch (Exception e) {
                     e.printStackTrace();
                 } finally {
                        counter++; 
                 }
          } 
     }

     public void run () {
            try {
                 doStuff();
            } catch (IOException e) {
                 e.printStackTrace();
                 setStopAll();
            } catch (Exception e) {
                 e.printStackTrace();
            }
     }

     public static void main (String[] args) throws Exception { 
          final int MAX_THREADS = 500;
          final int MAX_HOLDING_THREADS = 5;
            int loopCount = 0;
            Thread mainThread = Thread.currentThread();
            for (int m=0; ; m++) {
                 Thread t[] = new Thread[MAX_HOLDING_THREADS];
                 for (int n=0; n<t.length; n++) {
                        t[n] = new Thread(new ArchiveFileTest());
                        t[n].start();
                        if ((m+1)*(n+1)==MAX_THREADS) {
                             System.out.println("\n" + MAX_THREADS + " reached... \nMain Sleeping for some mins..."); 
                             loopCount++;
                             try {
                                    t[n].join();
                                    System.out.println("\nMain is back... (" + loopCount + ")");
                             } catch (InterruptedException e) {
                                    e.printStackTrace();
                                    setStopAll();
                             }
                             m = 0;
                        }
                 }
            }
     }
}
  • 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-05-25T02:11:20+00:00Added an answer on May 25, 2026 at 2:11 am

    I don’t think your application will ever stop. You’ve got an infinite loop in the main method:

    for (int m=0; ; m++) {
        ....
    }
    

    Note, setting m=0 inside the body won’t break the loop, so I think you’ll never end even if you have no file. It then continuously reads all zip/jar/war/rar files in the directory (choosing the file based on a rotating counter myNumber is not very maintainable), but never exits the loop.

    If you’re requirement is to read ZIP files using a number of threads, then I would go about it a different way.

    1. Create a Set of files which you want to look at.
    2. Create a ThreadPoolExecutor to create a fixed pool of 5 threads
    3. Iterate over the set of files and create a new Runnable which does the Zip Extraction (though I’m not quite sure why you read the first 10000 bytes of a ZIP entry and then don’t do anything with it), and call the execute method. That will use the thread pool to process 5 files at a time.
    4. After submitting all the runnables Use the shutdown method, which will wait for all submitted tasks to finish, and the shutdown the thread pool.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the scenario: I have a multi threaded java web application which is running
I have a multi-threaded application that has a centrlaised list that is updated (written
I have a multi-threaded Windows C++ app written in Visual Studio 6. Within the
I have written an AIR Application that downloads videos and documents from a server.
I have a homework assignment to write a multi-threaded sudoku solver, which finds all
I am writing a multithreaded program in java. I have written something like this
I have never written multi-threaded code before (barring a few basic backgroundworker tricks) and
I have written an application which is using NHibernate to provide the underlying object
I've written a multi-project .vstemplate file, which works alright, except that the projects have
What follows is a regular expression I have written to match multi-line pre-processor macros

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.