Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.j256;
- public class WordTearingExample {
- private static final int NUM_THREADS = 100;
- private static final int ITERS = 100000000;
- public static void main(String[] args) {
- Thread[] threads = new Thread[NUM_THREADS];
- byte[] counts = new byte[NUM_THREADS];
- for (int i = 0; i < NUM_THREADS; ++i) {
- threads[i] = new Thread(new Inner(counts, i));
- threads[i].start();
- }
- }
- private static class Inner implements Runnable {
- private final byte[] counts;
- private final int id;
- public Inner(byte[] counts, int id) {
- this.counts = counts;
- this.id = id;
- }
- public void run() {
- for (int i = 0; i < ITERS; i++) {
- if (counts[id] != (byte) i) {
- System.err.println("Word-Tearing found: " + "counts[" + id + "] = " + counts[id] + ", should be "
- + i);
- return;
- }
- // NOTE: no synchronization
- counts[id] = (byte) (i + 1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement