Using Events with Threaded apps
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mike_Lemmerling
Posted On:   Monday, November 18, 2002 11:56 AM

I have an app that uses threads to download files. We have 1 thread per download, and I want a way to notify the main thread that each download has completed, or even its status before it completes. The Java Event Model seems entirely connected to GUI interfaces but my app is a console application. Can I use the Event model to communicate between the main thread and the download threads?

Re: Using Events with Threaded apps

Posted By:   Alex_Chaffee  
Posted On:   Tuesday, November 19, 2002 07:08 PM

Yes, you could use the Event model. It's not entirely connected to GUIs.


But instead, why not just use pointers? That's what they're for!



class Main {

void somethingHappened() {
System.out.println("Yow!");
}

}

class Downloader extends Thread {
Main main;
public Downloader(Main m) {
this.main = m;
}

public void run() {

downloadSomething();
main.somethingHappened();

}
}


Note that the somethingHappened() call will happen on the Downloader's thread; you may want to set a flag inside Main that's polled by a different thread, or use wait/notify, or...

Re: Using Events with Threaded apps

Posted By:   Edward_Harned  
Posted On:   Monday, November 18, 2002 12:33 PM

Forget the Event Model.



Rather then write it all again here, take a look at this article:

http://www-106.ibm.com/developerworks/java/library/j-rmiframe/



Yes, its about RMI, but most importantly its about controlling threads. The downloadable code has a section where you can run it as a non-RMI Server -- just what
you're looking for.

About | Sitemap | Contact