package io.pivotal.diskwriter;

import java.io.FileOutputStream;
import java.util.stream.IntStream;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DiskWriterApplication {

	private static String writers = System.getenv("WRITERS");
	private static int counter = 0;

	public static void main(String[] args) {
		startWriters();
		SpringApplication.run(DiskWriterApplication.class, args);
	}

	static class WriterRunnable implements Runnable {
		private int index;

		public WriterRunnable(int index) {
			this.index = index;
		}

		@Override
		public void run() {
			System.out.println("Starting runner #" + index);
			while (true) {
				byte dataToWrite[] = new byte[1024 * 1000];
				FileOutputStream out;
				try {
					out = new FileOutputStream("/tmp/" + index + ".dat");
					out.write(dataToWrite);
					out.flush();
					out.close();
					counter++;
				} catch (Exception e) {
					e.printStackTrace();
					System.exit(1);
				}
			}
		}

	}

	public static void startWriter(int index) {
		WriterRunnable runnable = new WriterRunnable(index);
		Thread t = new Thread(runnable);
		t.setPriority(Thread.MIN_PRIORITY);
		t.start();
	}

	public static void startWriters() {
		IntStream.range(0, Integer.parseInt(writers)).forEach(
				i -> startWriter(i));
	}

	@RequestMapping(value = "/")
	public String get() {
		return "Hello. You've got " + Integer.parseInt(writers)
				+ " writers who've written " + counter + "MB of data";
	}
}

@@ -58,16 +58,66 @@
 Stream;%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A
 import j
@@ -547,23 +547,52 @@
 tic 
-int counter = 0
+AtomicInteger counter = new AtomicInteger(0)
 ;%0A%0A%09
@@ -1180,10 +1180,26 @@
 nter
-++
+.incrementAndGet()
 ;%0A%09%09

Use atomic int for thread saftey