0

I'm looking for an efficient way to split a text file into a set of ArrayList. The text file is a Thread dump and I'd like to create a List for every single Thread. Every thread is separated by an empty line. For example, taken the following file:

"management-handler-thread - 66" prio=10 tid=0x00007fe960111000 nid=0x4cea waiting on condition [0x00007fe96c25c000]
   java.lang.Thread.State: TIMED_WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000006019cbbd0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:226)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2082)
        at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)

"management-handler-thread - 65" prio=10 tid=0x00007fe968185800 nid=0x4ce9 waiting on condition [0x00007fe96c35d000]
   java.lang.Thread.State: TIMED_WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000006019cbbd0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:226)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2082)
        at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)

The first List should contain:

"management-handler-thread - 66" prio=10 tid=0x00007fe960111000 nid=0x4cea waiting on condition [0x00007fe96c25c000]
   java.lang.Thread.State: TIMED_WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
. . .

And the second List should contain:

"management-handler-thread - 65" prio=10 tid=0x00007fe968185800 nid=0x4ce9 waiting on condition [0x00007fe96c35d000]
   java.lang.Thread.State: TIMED_WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
. . . .

What would you recommend to do that ? (Iteration, Regular expression ..) Thanks

3
  • Iterate over lines, insert depending on read data to proper list Commented Jul 4, 2015 at 20:51
  • How big is your input file? You most likely don't want to load a big log file into memory at once but use streams. Commented Jul 4, 2015 at 20:52
  • Why do you want to write the result into ArrayLists? You could also write them into separate files directly. Use a BufferedReader for the input and PrintWriters for the output files and each time you encounter an empty line you write the current entry into the appropriate file. Commented Jul 4, 2015 at 21:07

1 Answer 1

3

No need for regular expressions, just read the file line by line...

List<List<String>> dumpedThreads = new ArrayList<>();
try (FileReader fr = new FileReader("path/to/thread-dump.txt")) {
    try (BufferedReader br = new BufferedReader(fr)) {
        List<String> thisThread = null;
        for (String line = br.readLine(); line != null; line = br.readLine()) {
            if (line.trim().length() == 0) {
                thisThread = null;
            } else {
                if (thisThread == null) {
                    thisThread = new ArrayList<>();
                    dumpedThreads.add(thisThread);
                }
                thisThread.add(line);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That will only work if thread-dump.txt is sorted by thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.