JavaMemoryModel: Word-tearing

From: Doug Lea (
Date: Sat Apr 15 2000 - 14:51:17 EDT

Bill: Consider adding something to your draft JSR request that
addresses word-tearing. To remind everyone of the issue, suppose you

class Tearer extends Thread {
  byte[] counts = new byte[1000]; // 1000 is arbitrary
  final int id;

  Tearer(int i) { id = i; }
  public void run() {
    int iters = 0;
    while (counts[id]++ < 100) ++iters; // 100 is semi-arbiratry
    assert iters == 100; // Uses upcoming assert syntax

  public static void main(String[] args) {
    for (int i = 0; i < 1000; ++i)
      new Tearer(i).start();

The reason that such programs might fail on some architectures is that
bytes are written only in 2/4/8/whatever-byte chunks. So, even
though each thread is reading/writing to/from an independent location,
the surrounding cells may be affected by the write. The independence
of locations means that the issue falls outside of the sorts of rules
we've been discussing.

Tearing would surely have to be outlawed for arrays of references.
But outlawing such effects of arrays of bytes/shorts/... would require
padding cells to the smallest writable unit on multiprocessor VMs,
which probably no one would want.

The problem can also affect writes to adjacent fields (as opposed
to adjacent array elements).

Doug Lea, Computer Science Department, SUNY Oswego, Oswego, NY 13126 USA 315-341-2688 FAX:315-341-5424  
JavaMemoryModel mailing list -

This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:25 EDT