Here are your tools for writing thread-safe code.
Does the synchronized mechanism lock Threads out of the object in all critical regions or the object in just one critical region? It is the first. All critical sections locked with the same object are blocked. Keep in mind that you can use an object other than the one you are working on as the locking object. This would let you for example allow two groups of critical code to be executing simultaneously, provided they locked on different objects, e.g. a dummy inner class locking object. Understand that the objects themselves are not locked. Other threads can access them with non-sychronised methods, even when they are locked. It also means you could have critical code acting on thousands of different objects funnelled to single processing stream by locking all the critical code on a single lock object. This would not be efficient, but would be theoretically possible.
Unfortunately you can’t use interrupt to abort just any i/o. It can really only be used for that when using the JDK 1.4 java.nio I/O. In particular when using a Channel that implements InterruptibleChannel, which includes channels for files, pipes and networking. Thread.interrupt is not guaranteed to wake up any thread blocked in any other I/O operation.
See the warning under Gotchas:Threads on why a sleeping task may never waken if somebody fiddles with the system clock setting while your thread is asleep.
// running on the same thread aRunnable.run();
If you do, run will be called like a normal method, with no new Thread created. You must create a Thread object and then call start on the Thread object instead.
// starting a Thread Thread thread = new Thread( aRunnable ); thread.start(); // which will then execute arunnable.run() on a separate Thread.
You are not on the Swing event thread when main first starts up! You are on it in your event listeners. You are tell if you are on it with:
// Either method will work in Java 1.3+ // to determine if you are running on the dispatch thread, // the same for AWT or Swing. // in Swing boolean onSwingThread = javax. swing.SwingUtilities. isEventDispatchThread(); // in AWT boolean onDispatchThread = java. awt.EventQueue. isDispatchThread(); // Dump info about the current thread, name, priority, group. System. out.println ( Thread. currentThread() ); // Dump name of the current thread System. out.println ( Thread. currentThread(). getName() );
One of the most common errors is to tie up the AWT/Swing event thread with some long running computation or even a sleep. Everything freezes up. The GUI can’t respond to mouse clicks and no repainting happens. You have to spin the time-consuming task off on its own thread or use a Timer.
is a Sun class that is not part of the JDK. It lets you convert some long-running event handling code into a separate thread with just a line of extra code.If you violate these rules, the entire system starts to behave unpredicably and irrationally. If by violating these rules, you manage to create two event-processing threads, life gets really interesting as they unpredictably fight with each other handling Swing events.
| thread 1 | thread 2 |
|---|---|
| load balance | |
| load balance | |
| add depositAmount2 | |
| store balance | |
| add depositAmount1 | |
| store balance |
If instead you wrote:
Then only one thread at a time could access the account record containing the balance field, and the code would have to run like this:| thread 1 | thread 2 |
|---|---|
| load balance | |
| add depositAmount1 | |
| store balance | |
| load balance | |
| add depositAmount2 | |
| store balance |
For example, imagine a banking system with a getBalance() method to examine the balance, and another withdraw( long withdrawAmount) method to update the a balance.
In a naive system, the teller might first do a transaction that calls the checkBalance() method to look at the balance, and if it has sufficient funds, calls the withdraw method to update the balance.However, between the method call to look at the balance and update the balance, another withdrawal transaction could come in and snaffle the funds. The withdrawal would improperly go ahead, leaving a correctly-computed overdraft.
| thread 1 | thread 2 |
|---|---|
| getBalance | |
| check balance big enough for withdrawal1 | |
| getBalance | |
| check balance big enough for withdrawal2 | |
| withdraw( withdrawalAmount2) | |
| withdraw ( withdrawalAmount1) |
For such code to work in a thread-safe way, the withdrawal method must do an atomic integral last-minute check on the balance, as well as an atomic increment on the balance such as the sample code for a withdrawal in the example on why synchronized is needed.
One trick you can use for static variable contention is to introduce static references to dummy lock objects new Object(). Instead of locking the whole class, you lock just one of the lock objects which represents access to some subset of the member variables. You can also use the techique for instance variables in objects with many threads competing for access.
![]() |
recommend book⇒Java Concurrency in Practice | ||
| paperback | kindle | ||
|---|---|---|---|
| ISBN13: | 978-0-321-34960-6 | B000RH0EPC | |
| ISBN10: | 0-321-34960-1 | ||
| publisher: | Addison-Wesley | ||
| published: | 2006-05-19 | ||
| by: | Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea | ||
| Bloch and Lea especially have very good reputations in concurrent programming. This is the dream team to write such a book. See the companion website with code examples, a sample chapter, errata etc.. | |||
![]() |
recommend book⇒Concurrent Programming in Java: Design Principles and Patterns, Second Edition | |
| paperback | ||
|---|---|---|
| ISBN13: | 978-0-201-31009-2 | |
| ISBN10: | 0-201-31009-0 | |
| publisher: | Prentice Hall | |
| published: | 1999-01-04 | |
| by: | Doug Lea | |
| 432 pages. You can read it online. This is the book on Threads and concurrent programming. The only problem with it is does not cover the new java.util.concurrent package which the author helped design. | ||
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.62] | The information on this page is for non-military use only. | ||
| You are visitor number 52,524. | Military use includes use by defence contractors. | ||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror) | ||
| http://mindprod.com/jgloss/threadsafe.html | J:\mindprod\jgloss\threadsafe.html | ||