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 6973203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:04:41+00:00 2026-05-27T17:04:41+00:00

I am sorting big file with by reading into chunks (Arraylist), sorting each arraylist

  • 0

I am sorting big file with by reading into chunks (Arraylist), sorting each arraylist using Collections.sort with custom comparator and writing the sorted results into files and then applying merge sort algorithm on all files.

I do it in one thread.

Will I get any performance boost if I start a new thread for every Collections.sort()?

By this I mean the following:
I read from file into List, when List is full I start a new thread where I sort this List and write to temp file.

Meanwhile I continue to read from file and start a new thread when the list is full again…

Another question that I have:

What is better for sorting:
1)Arraylist that I fill and when it’s full apply collections.sort()
2)TreeMap that i fill, I don’t need to sort it. (it’s sorts as I insert items)

NOTE: I use JAVA 1.5

UPDATE:
This is a code I want to use, the problem are that I am reusing datalines arraylist that is beeing used by threads and also I need to wait until all threads complete.
how do i fix?

int MAX_THREADS = Runtime.getRuntime().availableProcessors();
ExecutorService executor = Executors.newFixedThreadPool(MAX_THREADS);
List datalines = ArrayList();       
try {
   while (data != null) {
       long currentblocksize = 0;

    while ((currentblocksize <= blocksize) && (data = getNext()) != null) {
                                        datalines.add(data);
    currentblocksize += data.length();
   }                
    executor.submit(new Runnable() {                
       public void run() {
    Collections.sort(datalines,mycomparator);
    vector.add(datalines);
       }
   });
  • 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-27T17:04:42+00:00Added an answer on May 27, 2026 at 5:04 pm

    I suggest you to implement the following scheme, known as a farm:

                 worker0
    reader  -->  worker1  -->  writer
                 ...
                 workerN
    

    Thus, one thread reads a chunk from the file, hands it to a worker thread (best practice is to have the workers as an ExecutorService) to sort it and then each worker sends their output to the writer thread to put in a temp file.

    Edit: Ok, I’ve looked at your code. To fix the issue with the shared datalines, you can have a private member for each thread that stores the current datalines array that the thread needs to sort:

    public class ThreadTask implements Runnable {
        private List datalines = new ArrayList();
    
        public ThreadTask(List datalines) {
            this.datalines.add(datalines);
        }
    
        public void run() {
           Collections.sort(datalines,mycomparator);
           synchronized(vector) {
               vector.add(datalines); 
           }
        }
    }
    

    You also need to synchronize access to the shared vector collection.

    Then, to wait for all threads in the ExecutorService to finish use:

    executor.awaitTermination(30, TimeUnit.SECONDS);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi there I'm reading a string and breaking each word and sorting it into
I'm working with a very big text file (755Mb). I need to sort the
Trying to load a file into python. It's a very big file (1.5Gb), but
I have a big table and want to sort using tablesorter. The problem I'm
Is the sorting algorithm used by .NET's Array.Sort() method a stable algorithm?
I was looking into sorting tables by a column designated given some input, and
This recent question about sorting randomly using C# got me thinking about the way
Here is a simple sorting program of an ArrayList: ArrayList<String> list = new ArrayList<String>();
So Ive got a pretty big array of data and need to sort them
I have a pretty big web application that I created last year using ASP.NET

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.