HOME
Login
Change Info
Logout


TUTORIALS

C, C++
Win32
Java
Visual Basic
MFC
DCOM
Networking
C#
Perl
HTML
XML
ASP
PHP
Javascript
Other

DOWNLOADS
ITCLib
SourceVizor meets the notification, reporting, and admin needs of teams using Microsoft Visual SourceSafe.
Free 30-day trial!


Thread Synchronization in Java
How to Get True Multithreading in Java

by Christopher McGee

Questions, comments, suggestions - We want to hear from you!Comment on this article Get the Acrobat PDF File For This Article (32KB) Get Adobe Acrobat Reader
Most downloads under 500KB


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.


Previous Page
Return to beginning of article

Next Page


Featured Article

An Introduction to C#
By Joey Mingrone

Register Today!


100% FREE

Members enjoy these benefits:
Access to ITI Downloads
Access to more articles and tutorials
Optional weekly newsletter
And more...

Click here to register
Or
Click here to log in



© 2008 Interface Technologies, Inc. All Rights Reserved
Questions or Comments? devcentral AT iticentral DOT com
PRIVACY POLICY