This is my first attempt playing around with threading in java/processing. I have a simple animation of a bouncing ball while I attempt to launch a thread that loads a file and copies lat/lon coordinates to corresponding lat/lon float[] arrays.
The threading seems to begin without any issues, but as the coordinates are parsed and appended to float[] arrays, it eventually encounter a java.lang.OutOfMemoryError. It never seems to occur at the same point, so I don't think it's actually running out of memory. Plus I've been able to load all of the coordinates in and copy them to float arrays without threading.
Here is the threaded code:
Code:import java.lang.Thread;
LoadData f;
Bounce b;
float[] lat_coords = new float[0];
float[] lon_coords = new float[0];
void setup(){
size(200,200);
framerate(60);
smooth();
f = new LoadData("GLBOUNDS_MIF.MIF");
b = new Bounce();
}
void draw(){
background(0);
b.draw();
}
void mousePressed(){
f.start();
}
class LoadData extends Thread{
String filename;
String[] lines_all;
boolean record = false;
boolean regionstart = false;
boolean polystart = false;
LoadData(String filename_){
filename = filename_;
lines_all = loadStrings(filename);
}
void run(){
for(int i=0; i<lines_all.length; i++){
if(lines_all[i].startsWith("DATA")){
record = true;
}
if(record){
if(lines_all[i].startsWith("REGION")){
regionstart = true;
float percent = float(i)/float(lines_all.length) *100;
println(int(percent) + "%");
} else {
String[] coords = split(trim(lines_all[i]));
if(coords.length > 1){
lat_coords = append(lat_coords, float(coords[1])*-1);
lon_coords = append(lon_coords, float(coords[0]));
}
}
}
}
println(filename + " loaded");
}
}
class Bounce{
int x,y;
float xvec,yvec;
Bounce(){
x = int(random(0,width));
y = int(random(0,height));
xvec = int(random(0,5));
yvec = int(random(0,5));
}
void draw(){
if(x > width){
xvec *= -1;
} else if(x < 0){
xvec *= -1;
}
if(y > height){
yvec *= -1;
} else if(y < 0){
yvec *= -1;
}
x += xvec;
y += yvec;
ellipse(x,y,10,10);
}
}
The file GLBOUNDS_MIF.MIF is over 385 thousand coordinates comprising country and coastline boundaries. It's 8.5mb in size, but I went ahead and uploaded a copy incase someone was actually interested in running it.
http://www.datadreamer.com/muntadas/GLBOUNDS_MIF.MIF