4 Replies - 288 Views - Last Post: 10 October 2011 - 02:52 PM

#1 garnaout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-September 11

New to Multithreading

Posted 10 October 2011 - 07:49 AM

Hello all,

I have this logic in my application:

1- Read trace file that has animation in Jmonkey format
2- From simpleApp execute the cinematic line by line and create the Jmonkey animation

However the trace file could have up to 300,000 lines (or events since each line is a specific event) meaning that doing so in one shot is not efficient as it would take me up to 5 minutes to load all the events and then start executing them. After the loading, the animation runs fine.

In order to improve the loading time, I was suggested to use threads (I have never used multithreading but I know the concept and read the advanced multi-threading tutorial). I was hoping if there’s a way to initiate the animation after reading the first chunk of the file and at the same time, using threads read the second chunk of it so I don’t have to wait for it. This shouldn’t be too hard as all the spatials are created at time 0, and the rest of the file just moves, rotates, and accelerate/decelerate those spatials.

So far my design is

Jme3Cinematics.java –> contain the animation main engine, createSpatials(), rotateSpatials(), accelerateSpatials() etc

Adapter.java –> reads trace file and parses line by line and according to what’s read & calls the appropriate function from Jme3Cinematics class.


Question is how can I use multithreading to solve this issue? How can I divide the file into 2 parts (or probably more) and assign a thread for each?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: New to Multithreading

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 6463
  • View blog
  • Posts: 23,497
  • Joined: 12-June 08

Re: New to Multithreading

Posted 10 October 2011 - 08:28 AM

Wouldn't you need the file to be read sequentially for animation? I mean if you have three threads going wouldn't that mean the file or events or what ever get fired off out of order?
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3873
  • View blog
  • Posts: 11,407
  • Joined: 18-April 07

Re: New to Multithreading

Posted 10 October 2011 - 10:42 AM

Well one way to tackle this would be to create a buffering mechanism where you start up the program read lets say 10,000 lines, put them in a queue, start the animation with those as a second thread is fired to read the file (like 500 at a time) and put them in a queue.

I see you only needing two threads for this. One for the processing of the animation and a second who reads lines and appends it to the queue until the file is completely read. So you need to be able to start your thread using a function that can start 10,000 lines in on a file and then read chunks of lines at a time, append them to a queue that both threads can see and continue.

The one thing you would have to watch in this is that your animation doesn't eat up the lines faster than your second thread can append to them. If that is the case, you will need to increase the number of lines you initially read.

This is much like a streaming process using a buffer. You can start the animation without having fully loading everything yet.

:)
Was This Post Helpful? 0
  • +
  • -

#4 garnaout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-September 11

Re: New to Multithreading

Posted 10 October 2011 - 01:34 PM

Great! but the question is how to do so? any pseudocode you can attach or maybe a sample example you already have?


I already have the animation initiated and stops at 10,000 lines let's say with:
if(line_counter >= 10000)
{
 System.out.println("FIRST CHUNK COMPELTED - PROCEED TO THREAD");
 break; // stop reading file for now
}
else
{
 doStuff(); // animation methods are called from here
}



how do I proceed to initiate a thread at this point
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3873
  • View blog
  • Posts: 11,407
  • Joined: 18-April 07

Re: New to Multithreading

Posted 10 October 2011 - 02:52 PM

Ahhhh Java, I was waiting for you to specify a language... when you know as many as I do, it is hard to guess which you are talking about. Example I created for you...

import java.util.concurrent.*;

public class LoadFile {
	
	// Our queue which will contain all lines
	public static ConcurrentLinkedQueue<String> commandQueue = new ConcurrentLinkedQueue<String>();
	
	public static void main(String args[]) {
		LoadFile fileExample = new LoadFile();
		
		// Call initial load to load first 10,000 lines
		fileExample.initialLoad();
		
		// Start a thread which will then go about loading items into our queue
		Thread t = new Thread(new Runnable()
		{
		     public void run()
		     {
		          // Add to queue here in chunks (think of a function that takes a start line and number of lines to call)

		          // You can access commandQueue here to add to it.
            	
		     }
		});
		
		// Start the thread
		t.start();	
	}
	
	public void initialLoad() {
		System.out.println("Load 10,000 lines here into queue!");
	}
}



Play with this and in your inner runnable run method you would have a loop that would either call a function to load the lines from a file and read the file itself and add to it (I recommend the function).

Now your program just needs to read the queue item by item and process the animation. :)

This post has been edited by Martyr2: 10 October 2011 - 02:53 PM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1