• Post Reply Bookmark Topic Watch Topic
  • New Topic

Applying threads in business applications

 
Aldre Balboa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I do threads in business web applications?

I read topics on Java threads but it seems its one of those "nice to know features" which I may rarely if ever program with in my line of work. I have just recently been involved in a Java web project for an ordering system and it was a good learning experience - actually started Java 6 months ago (prior to this I did VB programming for some time). I am picking up on OOP concepts and the benefit I see in OOP is in the better organization of code.

I encountered threads when trying to understand the concepts behind connection pooling. I was also able to borrow a good book SCJD by M. Habibi and liked his discussion on threads there. I also read other articles. I get the feeling though that thread programming seem somewhat suited to making "tools".

I am just thinking how does one apply thread programming in ordinary business applications such as inventory, orders, sales, etc..? I just think that if I start in this I'd be able to "really" appreciate threads better?
[ May 02, 2005: Message edited by: Aldre Balboa ]
 
Jon Egan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aldre,

If what you've done so far in Java is primarily web development, it is not surprising that you wouldn't see the use for defining your own threads. Your code in a web application should be somewhat linear, handling a single response at a time from a particular starting point.

But think of it this way - the web server in which your code is running is definitely managing threads - so that the web server can respond to more than one request at a time. It will have more than one thread running your code, one for each request, and if the code is properly synchronized, etc, they should never need to know about each other.

The web server has to have a main process that is listening for requests, and then, each time it gets a request, fire off a thread to "handle" the request. Then it goes back to listening for the next request - before the first request is finished being handled. That is the type of situation where you would need to manage threads.

Another is in batch processing, if you have some operations that are slow and not CPU-intensive (DB queries/updates, file I/O), and others that are fast and require the CPU (calculations, hash lookups, ...). If you split up the batch job into chunks, and have one thread per chunk, they can all work on their "chunk" at the same time - while one is waiting for some I/O to finish, another can be doing the in-memory calculations, etc. Then the whole job gets done faster.

A third example is in GUI programming, where you want the GUI to stay responsive (at least, continue to redraw itself so it doesn't look "locked up"), while the code is doing some time-intensive operation (maybe connecting to a DB on a server to get a set of records for browsing). So the GUI code goes on one thread, and "processing" is in another.

Hope that helps.
-- Jon
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My real life business domain would seem to be very boring financial stuff. But even here we sometimes use threads to run two data requests at the same time. Since the code in these requests spends most of its time waiting for the partner system to respond we can run two requests in very nearly the same time as one so the user sees results sooner.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jon said, you've been using threads all along, but because Java abstracts it well, developers tend to be isolated from them and don't need to be conscious of it. The web server creates and manages multiple threads, and the connection pool you use uses synchronization (exclusion locks that block other threads from entering sections of code) to ensure that multiple threads don't corrupt its pool data structures.

To get your hands dirty with threads, I recommend starting with Sun's threads tutorial. It will expose you to the small thread API and show you how to create a simple threaded program. Once you've done that, create a few more complex programs on your own (or find more tutorials). If you really want to get a grip on threads, pick up Java Threads by Henry Wong, who posts here quite often. Max Habibi and a bunch of other Java book authors hang out at the Ranch as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!