Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

An Introduction to Java Thread Programming

Multi-threaded programming is all but essential for complex problem solving and effective user interfaces. Yet, to many programmers, it remains an arcane science. Java makes threaded programming simple due to its portable nature.


advertisement
he concept of threaded programming isn't new; most modern operating systems provide support for threads in some fashion. However, as widespread as threads are, the number of developers who have worked with them is fairly small. The cause of the problem is not clear—perhaps because threaded code is virtually unportable; perhaps because threaded programming requires techniques that aren't familiar to many developers. However, threaded programming is almost essential. Any substantial program is multithreaded. Java, by its very nature, solves portability problems, and the thread support in Java is so elegantly simple that any developer can pick up the mechanisms of thread programming with ease.

Threads, Defined
What precisely is a thread, however? A "thread" is shorthand for a "thread of control." At its simplest, this is just a section of code executed independently of other threads of control within a single program. We're all familiar with multitasking wherein an operating system can run more than one program at a time. A thread is much the same, but, conceptually, exists at a process level. That is, one process may be divided into multiple threads, executing simultaneously, all performing discrete tasks that contribute to the overall program. The popularity of threads increased when graphical user interfaces became standard for desktop computers, as they gave the illusion of better performance. The programs didn't really run any faster, but separate threads could be used to (for example) print a file and receive new keystrokes from the user. No longer would users need to wait for a document to print before they could continue working, as these programs invoke a separate thread to perform the printing task in the background.

Another advantage of a thread is its improved handling of blocking I/O. In Java, just like most programming languages, input is read from a stream. If an attempt is made to read data when none is currently available, the program will simply wait (block) until input becomes available and the read request is fulfilled. In most cases, this behavior is undesirable. While blocked, the appearance to the user will be that the program has hung, as any actions appear to be ignored. By using multithreading, the Java program can create a separate thread to read data from the desired input stream. And if this thread blocks, all other threads remain unaffected and can still perform servicing and processing in the meantime. There is a potential problem with thread programming that requires special handling. See the sidebar "Busy Waiting."



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date