i have code use googleseach API
I want to use Thread to improve speed of my program. But i have a problem
here is code
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.yahoo.search.WebSearchResult;
/**
* Simple Search using Google ajax Web Services
*
* @author Daniel Jones Copyright 2006 Daniel Jones Licensed under BSD open
* source license http://www.opensource.org/licenses/bsd-license.php
*/
public class GoogleSearchEngine extends Thread {
private String queryString;
private int maxResult;
private ArrayList<String> resultGoogleArrayList = null;
public ArrayList<String> getResultGoogleArrayList() {
return resultGoogleArrayList;
}
public void setResultGoogleArrayList(ArrayList<String> resultGoogleArrayList) {
this.resultGoogleArrayList = resultGoogleArrayList;
}
public String getQueryString() {
return queryString;
}
public void setQueryString(String queryString) {
this.queryString = queryString;
}
public int getMaxResult() {
return maxResult;
}
public void setMaxResult(int maxResult) {
this.maxResult = maxResult;
}
// Put your website here
public final static String HTTP_REFERER = "http://www.example.com/";
public static ArrayList<String> makeQuery(String query, int maxResult) {
ArrayList<String> finalArray = new ArrayList<String>();
ArrayList<String> returnArray = new ArrayList<String>();
try {
query = URLEncoder.encode(query, "UTF-8");
int i = 0;
String line = "";
StringBuilder builder = new StringBuilder();
while (true) {
// Call GoogleAjaxAPI to submit the query
URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?start=" + i + "&rsz=large&v=1.0&q=" + query);
URLConnection connection = url.openConnection();
if (connection == null) {
break;
}
// Value i to stop while or Max result
if (i >= maxResult) {
break;
}
connection.addRequestProperty("Referer", HTTP_REFERER);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String response = builder.toString();
JSONObject json = new JSONObject(response);
JSONArray ja = json.getJSONObject("responseData").getJSONArray("results");
for (int j = 0; j < ja.length(); j++) {
try {
JSONObject k = ja.getJSONObject(j);
// Break string into 2 parts: URL and Title by <br>
returnArray.add(k.getString("url") + "<br>" + k.getString("titleNoFormatting"));
}
catch (Exception e) {
e.printStackTrace();
}
}
i += 8;
}
// Remove objects that is over the max number result required
if (returnArray.size() > maxResult) {
for (int k=0; k<maxResult; k++){
finalArray.add(returnArray.get(k));
}
}
else
return returnArray;
return finalArray;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
this.resultGoogleArrayList = GoogleSearchEngine.makeQuery(queryString, maxResult);
System.out.println("Code run here ");
}
public static void main(String[] args)
{
Thread test = new GoogleSearchEngine();
((GoogleSearchEngine) test).setQueryString("data ");
((GoogleSearchEngine) test).setMaxResult(10);
test.start();
ArrayList<String> returnGoogleArrayList = null;
returnGoogleArrayList = ((GoogleSearchEngine) test).getResultGoogleArrayList();
System.out.print("contents of al:" + returnGoogleArrayList);
}
}
when i run it, it can run into run method but it don’t excute make query methor and return null array.
when i do’t use Thread it can nomal .
Can you give me the reason why ? or give a sulution
Thanks
One of the main problems is that you didn’t wait for the asynchronous computation to complete. You can wait by using
Thread.join(), but it’ll be even better if you use aFuture<V>, such as aFutureTask<V>instead.API links
java.util.concurrent(contains many high level concurrency utilities)interface Future<V>(represents result of asynchronous computation)interface RunnableFuture<V>(aFuturethat isRunnable)class FutureTask<V>(implementation that wraps aCallableorRunnableobject)interface Executor(“normally used instead of explicitly creating threads”)class Executors(provides factory and utility methods)Tutorials and lessons
See also
waitandnotify