
import traildb.*;

import java.io.FileNotFoundException;

class Parallel implements Runnable {
	TrailDB tdb;
	int sessionLimit;
	Parallel(TrailDB tdb, int sessionLimit) {
		this.tdb = tdb;
		this.sessionLimit = sessionLimit;
	}
	public void run() {
		System.out.println("Session Limit: " + sessionLimit);
		TrailDBCursor cursor = tdb.cursorNew();
		long n = tdb.numTrails();
		long totalSessions = 0;
		long totalEvents = 0;

		for (long i = 0; i < n; i++) {
			TrailDBEvent event;
			cursor.getTrail(i);
			event = cursor.next();
			long prevTime = event.timestamp;
			long numSessions = 1;
			long numEvents = 1;
			while ((event = cursor.next()) != null) {
				if (event.timestamp - prevTime > sessionLimit)
					numSessions++;
				prevTime = event.timestamp;
				numEvents++;
			}
			totalSessions += numSessions;
			totalEvents += numEvents;
		}
		cursor.free();
		System.out.println("Session Limit: " + sessionLimit + " Trails: " + n + " Sessions: " + totalSessions + " Events: " + totalEvents);
	}
}

class ThreadedExample {

	public static void main(String[] args) throws FileNotFoundException {
		TrailDB tdb = new TrailDB("wikipedia-history-small.tdb");

		for (int i = 0; i < 8; i++) {
			Thread thread = new Thread(new Parallel(tdb, 60 * (i + 1)), "" + i);
			thread.start();
		}
	}
}

@@ -862,16 +862,17 @@
 free();%0A
+%0A
 %09%09System
@@ -1096,16 +1096,38 @@
 xception
+, InterruptedException
  %7B%0A%09%09Tra
@@ -1180,16 +1180,65 @@
 l.tdb%22);
+%0A%09%09int n = 8;%0A%09%09Thread%5B%5D threads = new Thread%5Bn%5D;
 %0A%0A%09%09for 
@@ -1253,17 +1253,17 @@
  0; i %3C 
-8
+n
 ; i++) %7B
@@ -1348,21 +1348,156 @@
 read
-.start();%0A%09%09%7D
+s%5Bi%5D = thread;%0A%09%09%09thread.start();%0A%09%09%7D%0A%09%09for (int i = 0; i %3C n; i++) %7B%0A%09%09%09threads%5Bi%5D.join();%0A%09%09%7D%0A%0A%09%09System.out.println(%22Closing tdb%22);%0A%09%09tdb.close();
 %0A%09%7D%0A

Update ThreadedExample to close tdb