import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.concurrent.atomic.AtomicInteger;
public class Test {
public static void main(String[] args) throws InterruptedException {
Test test = new Test();
try {
System.setOut(new PrintStream(new FileOutputStream("D:/log_info.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
MyRunnable myRunnable = test.new MyRunnable();
for (int i = 0; i < 1000; i++) {
new Thread(myRunnable, "Thread" + String.valueOf(i)).start();
}
}
public class MyRunnable implements Runnable {
private AtomicInteger atomicInteger = new AtomicInteger(-1);
@Override
public void run() {
while(true) {
synchronized(atomicInteger) {
if(atomicInteger.get() >= 9999) {
return;
} else {
atomicInteger.addAndGet(1);
}
System.out.println(Thread.currentThread().getName() + ": **" + atomicInteger.get() + "**");
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}