FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   a problem with an external thread?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: a problem with an external thread?  (Read 444 times)
saito

tatsuyas1979 WWW Email
a problem with an external thread?
« on: Oct 19th, 2003, 5:00am »

i have been developing a googleapi interface.
as follows, i used a thread to search with googleapi to search google while animating images on a screen.
 
although it works with a normal java application, on proce55ing, it gets extremely slow and CPU usage reaches 100%.
 
my code seems to be pretty straight forward except the thread.
does using a thread in an external class somehow cause a problem?
 
the actuall code is as follows:
 
/*
 * easy-to-access threaded googleapi  
 * programmed by Tatsuya SAITO  
 *
 */
 
import com.google.soap.search.*;
import java.io.*;
 
public class pgoogle implements Runnable{
 
  Thread thread;
  String key;
 
  public String querystring;
 
  public int count;
  public String title[];
  public String url[];
  public boolean issearching = false;
     
  public pgoogle(String key){ // setting googleapi lisence key
    this.key = key;  
    this.thread = new Thread(this);
    thread.start();
  }
 
  public void run(){
    while(true){
 
 while(!issearching){} // wait until searching starts
 
 // start searching
 try {
   GoogleSearch s = new GoogleSearch();
   s.setKey(this.key);
   s.setQueryString(querystring);
   GoogleSearchResult r = s.doSearch();
   GoogleSearchResultElement results[]  = r.getResultElements();
   
   // stores results in member variables for simplification
   this.count = results.length;
   this.title = new String[results.length];
   this.url = new String[results.length];
   for(int i=0; i<results.length; i++){
     this.title[i] = results[i].getTitle();
     this.url[i]   = results[i].getURL();
   }  
 }catch(Exception e){e.printStackTrace();}
 
 issearching = false;
    }
  }
 
  public void searchby(String querystring){
    if(!issearching){
 this.querystring = querystring;
 this.issearching = true;
    }
  }
   
  public void relatedto(String querystring){
    if(!issearching){
 this.querystring = "related:" + querystring;
 this.issearching = true;
    }
  }
}
 
on proce55ing, i coded like this:
 
 
pgoogle g;
boolean f=false ;
 
void setup()
{
  g = new pgoogle("insert your googleapi key here");
  g.relatedto("www.proce55ing.net");
}
 
void loop(){
 
    // do some animation here //
 
    if(!g.issearching && !f){
   print(g.count); // prints the number of result
   f= true;   // only once.
    }
  }
}  
 
in this sample, it just displays the number of result because println seems to be very slow when it's used a lot.
 

-SAITO / tatsuyas@ucla.edu
fry


WWW
Re: a problem with an external thread?
« Reply #1 on: Oct 19th, 2003, 5:06am »

you need to use sleep() inside your Thread, otherwise it will starve your machine for resources, which is why you're jumping up to 100% cpu usage.
 
this is a java issue. it's probably just more likely to show up with processing since there's already another execution thread that's giving you trouble.  
 
(moving this over to 'programs', unless it turns up this actually is a p5 bug..)
 
saito

tatsuyas1979 WWW Email
Re: a problem with an external thread?
« Reply #2 on: Oct 19th, 2003, 6:44am »

it seems working after i added sleep().
thanks.
 

-SAITO / tatsuyas@ucla.edu
senior

89237688923768 WWW
Re: a problem with an external thread?
« Reply #3 on: Oct 20th, 2003, 9:09pm »

Just out of interest, how did you get the google api working?
If I plunk the googleapi.jar file in my code folder, I get the error: ". expected instead of this token", presumably referring to one of the files in the jar.
 
If I extract the jar file in my code folder, and remove the META-INF folder, it behaves properly. Did you have to do the same?
 
Also: I tried putting the jar in another folder and adding it to my classpath, but p5 couldn't find it... is the classpath ignored?
 
saito

tatsuyas1979 WWW Email
Re: a problem with an external thread?
« Reply #4 on: Oct 27th, 2003, 11:33am »

actually, i had same program regarding loading external class from p5.
i think what i did was to install expert version of p5 and put googleapi .jar file into lib/ext directory of java sdk. but, i just reinstalled my system and forgot to take a note on how i had the api work on p5.. and now i have the same problem..
 
is there any 'official' way to load external user defined classes from p5?
 

-SAITO / tatsuyas@ucla.edu
Pages: 1 

« Previous topic | Next topic »