![]() |
![]() |
|||||||
|
Login Change Info Logout
DOWNLOADS |
How to Get True Multithreading in Java by Christopher McGee
Introduction At ITI we write both Java applications and Java applets for a variety of clients and situations. Many of the programmers working on these projects come from a C++/MFC background. The transition is remarkably smooth (much smoother than the transition in the opposite direction would be), but one thing that caused problems for us early on is the Java threading model. In working with other project teams in other companies, I have also noticed a misunderstanding of threading in Java. When I work with other programmers, the easiest way I have found to explain the Java threading model is this: If you use "synchronized" for threading, then Java doesn't do multithreading. Java lets you think you've implemented multi-threaded processing, but in practice what Java does is disable multithreading. Instead, it queues up process threads, then executes them serially. This isn't multithreading. Rather, it is a scheme that Java uses to manage its inability to deliver true multithreading. The scheme is based on a concept called critical sections. What critical sections do is protect against more than one thread at a time entering a section of code. This concept is flawed because the code is not what you need to protect. You need to protect the underlying resource, which, if changed by more than one thread at a time, will blow up; or, if being read while being changed will yield corrupt data. If you want to avoid serial thread execution, you need to take active steps in every program you write. In this article, my goal is to discuss some of the problems I have seen and helped debug in multi-threaded Java code. I will start by demonstrating the most common form of the problem, and then move into the different techniques you can use in the Java language to solve and prevent multi-threading bugs. In addition, I will show you how to create true multi-threading applications in Java using a little-known feature hidden in the Object class. I will demonstrate a Boolean Lock class that we use in most of our multi-threaded applications here at ITI.
|
|
|
|||||||||||||||||||||||||
|
Questions or Comments? devcentral AT iticentral DOT com PRIVACY POLICY |