程序冻结,多线程? Locksupport.park方法

时间:2015-08-22 06:37:39

标签: java multithreading locking

我试图从GooglePlaces API中提取企业数据。它最初工作,但在制作一个特定的方法来提取地点(而不是在主方法中)之后,程序就会崩溃。对它进行debbuged时,它会被卡在一个名为park的java方法中(在java的LockSupport类下)。阅读它,它说当有超过1个线程并且存在同步问题时会发生这种情况。我对此非常陌生,我不知道如何在我的代码中解决这个问题。在我看来,这段代码中只有一个帖子,但我很确定我错了。请帮忙。它在" for"中崩溃评论如下。非常感谢!

package laundry;

import java.util.ArrayList;
import java.util.List;
import se.walkercrou.places.GooglePlaces;
import se.walkercrou.places.Param;
import se.walkercrou.places.Place;
import java.io.FileWriter; //add to import list
import java.io.IOException;
import java.io.Writer;
import static java.lang.Math.sqrt;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.location.Coordinates;
import se.walkercrou.places.exception.GooglePlacesException;
import se.walkercrou.places.exception.RequestDeniedException;


public class Laundry {
public static void main(String[] args) throws IOException {

    List<Place> detailedPlaces = new ArrayList<>();
    List<double[]> circlesPoint = new ArrayList<>();
    double radio = 10;
    Coordinates startingPoint = new Coordinates     (-38.182476,144.552079,0);//geelong south west corner of the grid
    Coordinates finalPoint   = new Coordinates(-37.574381,145.415879,0); //north east of melbourne
    GooglePlaces cliente = new GooglePlaces("keyof googlplaces");
    MyResult result1=exploreGrid(startingPoint,finalPoint, radio, detailedPlaces, circlesPoint,cliente); 
    writeResultsCircles(result1.getPoints(),"c:\\outputCircles.txt" );
    writeResultsPlaces(result1.getPlaces(), "c:\\outputPlaces.txt");
}   

private static MyResult exploreGrid(Coordinates SWpoint,Coordinates NEpoint,double rad, List<Place> lugares, List<double[]> points,GooglePlaces client){
    int iterationRow=0;
    Coordinates workingPoint = new Coordinates(SWpoint.getLatitude(),SWpoint.getLongitude(),(float) 0.0);
    List<Place> places = new ArrayList<>();
    while (workingPoint.getLatitude()<NEpoint.getLatitude()){
        while (workingPoint.getLongitude()<NEpoint.getLongitude()){
            try {
                places = client.getNearbyPlaces(workingPoint.getLatitude(), workingPoint.getLongitude(), rad*1000,GooglePlaces.MAXIMUM_RESULTS ,Param.name("types").value("laundry"));
                if (places.size()==60){//si se llega al tope de resultados de getNearbyPlaces
                    iterationRow=1;
                }
                for (Place place : places) {
                     lugares.add(place.getDetails());//here is where it crashes
                }
            }catch (GooglePlacesException ex) {   
                System.out.println(ex.getCause());
            }

            double[] prePoint = {workingPoint.getLatitude(),workingPoint.getLongitude(),rad};
            points.add(prePoint);
            workingPoint.setLongitude(workingPoint.getLongitude()+rad*sqrt(3)*0.01134787);
        }
        iterationRow++;
        if (isEven(iterationRow)){
            workingPoint.setLongitude(SWpoint.getLongitude()); 
        } else {
            workingPoint.setLongitude(SWpoint.getLongitude()+rad*sqrt(3)*0.01134787/2); 
        }

        workingPoint.setLatitude(workingPoint.getLatitude()+rad*3/2*0.00899416);
    }
    return new MyResult(lugares,points);
}

}    

0 个答案:

没有答案