Take the 2-minute tour ×

I have two classes in java that need to run at the same time - A Crawler class ( that basically implements a web crawler, and keeps printing out urls as it encounters them ), and an Indexer class, which as of now, is supposed to simply print the urls crawled.

For this, my Indexer class has a Queue :

public static Queue<String> urls = new LinkedList();

And in the toVisit() function of my Crawler class, I have the following :

Indexer.urls.add( url ) // where url is a String

The Crawler is working totally fine, since it prints out all the urls that it has encountered, but for some reason, these urls do not get added to the Queue in my Indexer class. Any idea why this may be the case ?

The toVisit() method from Crawler.java is as follows :

public void visit(Page page) {
            int docid = page.getWebURL().getDocid();
            String url = page.getWebURL().getURL();
            String domain = page.getWebURL().getDomain();
            String path = page.getWebURL().getPath();
            String subDomain = page.getWebURL().getSubDomain();
            String parentUrl = page.getWebURL().getParentUrl();

            System.out.println("Docid: " + docid);
            System.out.println("URL: " + url);
            System.out.println("Domain: '" + domain + "'");
            System.out.println("Sub-domain: '" + subDomain + "'");
            System.out.println("Path: '" + path + "'");
            System.out.println("Parent page: " + parentUrl);

            Indexer.urls.add( url );


Code from my Indexer class :


public static void main( String[] args )
   while( urls.isEmpty() )
            //System.out.println("Empty send queue");

        System.out.println( urls.poll() );

share|improve this question
You should post a SSCCE. Also if the 2 operations run in different threads you will need some sort of synchronization, or even better, to use a thread safe queue implementation. –  assylias Mar 30 '12 at 9:18
So you are running two different applications (processes) - So there is no chance to access the static variable of the other application. –  powerMicha Mar 30 '12 at 9:23
agree with others. write a simple main which starts the 2 classes as a thread. –  BigMike Mar 30 '12 at 9:24
Ok, so if start both as threads in a single main(), then static variables can be accessed right ? –  arya Mar 30 '12 at 9:27
Yes. I suggest you to change your two main classes in order to implement Runnable and have a third class having just the main function which allocates the threads and starts them. –  BigMike Mar 30 '12 at 9:29
show 8 more comments

1 Answer

up vote 0 down vote accepted

Okay, so I solved my problem by doing as suggested by BigMike. I implemented the Runnable interface in my two classes, and then ran those 2 classes as threads within the main function of a new third class.

Thanks everyone for all your help ! :)

share|improve this answer
add comment

Your Answer


