I created 10 thread and it take lots of time to doing computation as compared to looping. The most of the time taken by the thread creation. So i want to reduce the time to create the thread. Is there any solution for reducing the time to create a thread?
This call the thread and thread creation takes large amount of time. I want to reduce that time or if any other way to make execution fast.
class ThreadCheck{
public static int e = 30;
private static int minpt = 1;
private static List<Point> Neighbours = new CopyOnWriteArrayList<Point>();
public static void main(String[] s){
getList();
}
private static void getList() {
List<Point> newList = new CopyOnWriteArrayList<Point>();
newList.clear();
Point p = new Point(10, 15);
try{
FileInputStream fis = new FileInputStream("TotalFollowers.txt");
BufferedReader textReader = new BufferedReader (new InputStreamReader(fis));
String aLine;
while( (aLine = textReader.readLine())!= null )
{
StringTokenizer st = new StringTokenizer(aLine, "\t");
Point np = new Point(Double.parseDouble(st.nextElement()+""), Double.parseDouble(st.nextElement()+""));
newList.add(np);
if(newList.size() == 3000){
System.out.println(System.nanoTime());
ParallelVisit pv = new ParallelVisit(newList, p);
pv.start();
System.out.println(System.nanoTime());
newList = new CopyOnWriteArrayList<Point>();
}
}
new ParallelVisit(newList, p).start();
}catch(Exception e){
System.out.println(e);
}
}
public static synchronized void addNeighbour(Point p){
Neighbours.add(p);
}
}
This is the Thread class:
class ParallelVisit extends Thread{
List<Point> pointList;
Point p;
public ParallelVisit(List<Point> pointList, Point p){
this.pointList = pointList;
this.p = p;
}
public static double getDistance (Point p, Point q)
{
double dx = p.getX() - q.getX();
double dy = p.getY() - q.getY();
double distance = Math.sqrt (dx * dx + dy * dy);
return distance;
}
public void run(){
try{
Iterator<Point> points = pointList.iterator();
while(points.hasNext()){
Point q = points.next();
if(getDistance(p,q) <= ThreadCheck.e){
ThreadCheck.addNeighbour(q);
}
}
} catch(ConcurrentModificationException e){
System.out.println(e);
} catch(Exception e){
System.out.println(e);
}
}
}