Free Programming Books
Free download ebooks on computer and programming | |||
Free Java ebook "Taming Java Threads" Sample ChapterTaming Java Threads Download chapterFree downloads Chapter 10: If I Were King: Fixing Java's Threading Problems Learning how to write multithreaded applications is the key to taking full advantage of the Java platform. In Taming Java Threads, well-known columnist and Java expert Allen Holub provides Java programmers with the information they need to write real multithreaded programs-programs with real code. Holub provides an in-depth explanation of how threads work along with information about how to solve common problems such as deadlocks and race conditions. He not only explains common problems, but also provides the uncommon solutions that mark the difference between production-level code and toy demos. Topics covered in this book include the architecture of threads; the mutex and lock management; condition variables and counting semaphores; timers, alarms, and swing thread safety; observers and multicasters; singletons, critical sessions, and reader/writer locks; threads in an object-oriented world; and object-oriented threading architectures. While it is essential to build support for threading into a Java program from the very beginning, most books on the subjects of Java-UI construction and Java networking barely touch on threading topics. Along with being a basic Java reference, this book is a must-read for any Java developer. If I Were King: Fixing Java's Threading ProblemsIN A WAY, THE PREVIOUS CHAPTERS are a litany of everything wrong with the Java threading model and a set of Band-Aid solutions to those problems. I call the classes Band-Aids because the problems addressed by the classes should really be part of the syntax of the Java language. Using syntactic methods rather than libraries can give you better code in the long run since the compiler and JVM working together can perform optimizations that would be difficult or impossible with a library approach. In this chapter, I want to approach the threading problem in a more positive light by suggesting a few changes to Java that would provide solutions to those problems. These proposals are very tentative-they are just one person's thoughts on the matter, and they would need a lot of work and peer review before being viable. But they're a start. The proposals are also rather bold. Several people have suggested subtle, and minimal, changes to the Java-Language Specification (JLS) to fix currently ambiguous JVM behavior, but I want more sweeping improvement. On a practical note, many of my proposals involve the introduction of new keywords to the language. Though the usual requirement that you don't want to break existing code is certainly valid, if the language is not to stagnate and thus become obsolete, it must be possible to introduce keywords. In order to introduce keywords that won't conflict with existing identifiers, I've deliberately used a character ($) which is illegal in an identifier. (For example, rather than task). A compiler command-line switch could perhaps enable variants on these keywords that would omit the dollar sign. The TaskThe fundamental problem with Java's threading model is the fact that it is not in the least bit object oriented. A thread is effectively nothing but a procedure [run()] which calls other procedures. Notions of objects, asynchronous versus synchronous messages, and the like, are simply not addressed. One solution to this problem is the Active_object class presented in Chapter 9, but a better solution would be to modify the language itself to support asynchronous messaging directly. The asynchronous messages running on an Active Object are effectively synchronous with respect to each other. Consequently, you can eliminate much of the synchronization hassles required to program in a more procedural model by using an Active Object. | |||