Skip to main content

Real World Applications of Java Threading

2 replies [Last post]
Joined: 2009-01-21
Points: 0

Would anybody be able to give some specific examples of when/where Java Multithreading is used in real world examples. I'd like something a bit nmore detailed than 'IO Processing' or "Swing Event handling".
I want to write some such apps for an interview I am doing and I'd like some detail of practical applications of java threading.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2009-03-02
Points: 0

A good example that I recently read about was syntax highlighting. Imagine a code editor: You wish this editor to load very large files quickly, but also want to have syntax-aware coloring.

Enabling syntax-highlighting for a non-trivial language usually means more than just a series of regular expressions. You typically have to parse the code (not entirely, of course, just enough to fit your demands) and generate some sort of tree that represents the logical structure.

To maximize performance, you could have the parsing done in a separate thread. The main thread would perform no parsing, and just render the text in its native, uncolored form. You would create a second thread that's actually parsing the code and creating a syntax-aware tree. Once the second thread is complete, it replaces the initial raw-text tree with its own, and voila, you have highlighted text.

This example could be extended even further by having the second thread persist. IDEs typically have syntax highlighting, especially in Java, that is very complex - being able to recognize invalid method names, missing imports, etc, all on demand. The changes you make to the document shouldn't cause the editor to freeze up as it rebuilds the entire project.

If you're looking for an example of multithreading specifically in Java, then I'd assume you want some application of java.util.concurrent. The first example that came to my head involves CountDownLatch.

CountDownLatch is a simple beast that allows you to easily wait on many threads, only acting whenever the last of these is complete. Its mechanism is simple:

- If you want to wait on the Latch, you call await(), and your thread will only be awakened when the threads you're waiting on have finished their tasks.
- If you are a worker, then you'll call countDown() on the Latch whenever you're done with your work.

Once CountDownLatch reaches zero, the waiting threads are released. There's plenty of examples to use here. One would be having many worker threads performing some complicated operation on an image. You're not interested in the image until every worker has completed its task, so CountDownLatch is ideal for this scenario.

Its conceivable that you could use CountDownLatch to wait on two separate network requests, if the data they're sending back is not useful separately. An example would be validating the credit card number with one request, and validating the rest of the form with the other.

java.util.concurrent is just another package, but the magnitude that it simplifies multithreaded programming is difficult to overstate.

At any rate, I hope this is what you're looking for :)

Joined: 2006-07-09
Points: 0

A classic example is an application that contains it's own publish/subscribe implementation. For example, lets say you have a security application that requires creating very strong RSA public/private key pairs (4096 bit). When the application starts up, a pool of key pairs are generated. When a client attaches to the application it first is given a key pair and all transactions after that are expected to be encrypted. After passing out the key pair, a thread is started to replace the used key pair. This can take several seconds, but when it completes, the output is stuffed into the pool/queue, available for the next user.