站内搜索: 请输入搜索关键词
当前页面: 图书首页 > Java Threads, Third Edition

5.4 Summary - Java Threads, Third Edition

Previous Section  < Day Day Up >  Next Section

5.4 Summary

In this chapter, we've examined some advanced techniques for synchronization. We've learned about the Java memory model and why it inhibits some synchronization techniques from working as expected. This has led to a better understanding of volatile variables as well as an understanding of why it's hard to change the synchronization rules imposed by Java.

We've also examined the atomic package that comes with J2SE 5.0. This is one way in which synchronization can be avoided, but it comes with a price: the nature of the classes in the atomic package is such that algorithms that use them often have to change (particularly when multiple atomic variables are used at once). Creating a method that loops until the desired outcome is achieved is a common way to implement atomic variables.

5.4.1 Example Classes

Here are the class names and Ant targets for the examples in this chapter:

Description

Main Java class

Ant target

Swing Type Tester using atomic ScoreLabel

javathreads.examples.ch05.example1.SwingTypeTester

ch5-ex1

Swing Type Tester using atomic animation canvas

javathreads.examples.ch05.example2.SwingTypeTester

ch5-ex2

Swing Type Tester using atomic score and character class

javathreads.examples.ch05.example3.SwingTypeTester

ch5-ex3

Calculation test using thread local variables

javathreads.examples.ch05.example4.CalculatorTest

ch5-ex4


The calculator test requires a command-line argument that sets the number of threads that run simultaneously. In the Ant script, it is defined by this property:

<property name="CalcThreadCount" value="10"/>

    Previous Section  < Day Day Up >  Next Section