/** 0 - ordered, 1 - volatile, 2 - CAS */
private static final int WRITE_TYPE = Integer.getInteger("write.type",0);
/** any offset should be > 0 and < line size, and such that long write is not aligned */
private static final int OFFSET = Integer.getInteger("offset", 4);
private static final int CACHE_LINE_SIZE = 64;
/** allocate 3 cache lines sized block */
private static final long SOME_MEMORY = UNSAFE.allocateMemory(CACHE_LINE_SIZE * 3);
/** find the second line boundary in the block, so offset by up to a line size is still in the block (offsetting back)  */
private static final long LINE_BOUNDARY = CACHE_LINE_SIZE + SOME_MEMORY + (CACHE_LINE_SIZE - (SOME_MEMORY % CACHE_LINE_SIZE));
private static final long UNALIGNED_ADDRESS = LINE_BOUNDARY - OFFSET;
public static void main(String[] args) throws Exception {
  ...
  System.out.printf("unaligned address:%d cache line at:%d write type:%d\n",UNALIGNED_ADDRESS,LINE_BOUNDARY,WRITE_TYPE);
  UNSAFE.putLong(UNALIGNED_ADDRESS, 0);
  new Thread() {
    public void run() {
      while (true) {
        inlineMeRead();
      }
    }
    private void inlineMeRead() {
      long observed = UNSAFE.getLongVolatile(null, UNALIGNED_ADDRESS);
      if(observed != 0L && observed != -1L){
        System.out.printf("WTF:%d\n",observed);
        System.exit(-1);
      }
    }
  }.start();
  new Thread() {
    public void run() {
      while (true) {
        switch(WRITE_TYPE){
        case 0:
          inlineMeWriteOrdered();
          break;
        case 1:
          inlineMeWriteVolatile();
          break;
        case 2:
          inlineMeWriteCAS();
          break;
        default:
          System.exit(-2);
        }
      }
    }
    private void inlineMeWriteOrdered() {
      UNSAFE.putOrderedLong(null, UNALIGNED_ADDRESS, -1L);
      UNSAFE.putOrderedLong(null, UNALIGNED_ADDRESS, 0L);
    }
    private void inlineMeWriteVolatile() {
      UNSAFE.putLongVolatile(null, UNALIGNED_ADDRESS, -1L);
      UNSAFE.putLongVolatile(null, UNALIGNED_ADDRESS, 0L);
    }
    private void inlineMeWriteCAS() {
      UNSAFE.compareAndSwapLong(null, UNALIGNED_ADDRESS, 0L, -1L);
      UNSAFE.compareAndSwapLong(null, UNALIGNED_ADDRESS, -1L, 0L);
    }
  }.start();
  System.in.read();
  System.out.print("All is well!");
  System.exit(0);
}