Chaklader Chaklader - 15 days ago 6
Java Question

Can I use multi-threading to speed up a method operation?

I have the following model for the hotel data,

public class HotelData {

private String name;
private String address;
private String stars;
private String contact;
private String phone;
private String uri;


public HotelData(String name, String address, String stars, String contact, String phone, String uri) {
this.name = name;
this.address = address;
this.stars = stars;
this.contact = contact;
this.phone = phone;
this.uri = uri;
}

public HotelData() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getStars() {
return stars;
}

public void setStars(String stars) {
this.stars = stars;
}

public String getContact() {
return contact;
}

public void setContact(String contact) {
this.contact = contact;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getUri() {
return uri;
}

public void setUri(String uri) {
this.uri = uri;
}
}


And, I use the following method that uses the List rows to convert in the JSON format.

@Override
public void dataToJsonConverter() {

ObjectMapper mapper = new ObjectMapper();

try {
String jsonInString = "";

for (HotelData hotels : rows) {
jsonInString += mapper.writeValueAsString(hotels);
}

mapper.writeValue(new File(Constant.OUTPUT_LOC + "/result.json"), jsonInString);
// jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(hotelData);


} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("CONVERTED TO JSON");
}


However, the data volume is huge and its taking several seconds for the conversion. Can I use the multi-threading to speed up the process and how then? I don't have lot of experience with threading, so, please accept apology if the question is not appropriate. I just want to speed up the data conversation.

UPDATE: Surprisingly, the
StringBuilder
is about 25 times faster than the
String
. WOW

Answer

Multithreading isn't impossible, but will massively complicate this. If the methods are to be complicated anyway, I propose a method that's just faster to begin with. (And doesn't come with the baggage parallel solutions have to deal with)

The stringification is undoubtly so slow because of the reflection used in ObjectMapper. If you're up for some extra programming, the next solution is way faster. However it isn't as generic.

public interface JSONNable {
     String toJSON();
}


public class HotelData implements JSONNable {
    private String name;
    private String address;
    private String stars;
    private String contact;
    private String phone;
    private String uri;


    public HotelData(String name, String address, String stars, String contact, String phone, String uri) {
        this.name = name;
        this.address = address;
        this.stars = stars;
        this.contact = contact;
        this.phone = phone;
        this.uri = uri;
    }

    public HotelData() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getStars() {
        return stars;
    }

    public void setStars(String stars) {
        this.stars = stars;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String toJSON() {
        return String.format("{\"name\" : \"%s\", \"address\" : \"%s\", \"stars\" : \"%s\", \"contact\" : \"%s\", \"phone\" : \"%s\", \"uri\" : \"%s\"}", 
           name, address, stars, contact, phone, uri);
    }

    /* or any other iterable<HotelData> hotels, including an array */
    public static String hotelsToJSON(ArrayList<HotelData> hotels) {
        StringBuilder sb = new StringBuilder();

        sb.append('[');
        for (HotelData hotelData : hotels) {
            sb.append(hotel.toJSON());
            sb.append(',');
        }
        sb.insert(sb.length() - 1, ']');
        return sb.toString();
    }
}

Note that in creating a new interface and implementing the toJSON() method, there doesn't have to be a runtime analysis of the HotelData Object (which is slow). The difference in execution time costs you extra time programming though.

Comments