printfのスレッド同期への影響(未完了)


public class Test implements Runnable {
//
long times = 0;
int id = 0;
boolean flag = true;

Test() {
new Thread(this).start();
}

public void offAndCount() {
if (flag)
id++;
flag = false;
}

public void on() {
times++;
System.out.print(""); // , 。 , print ( :print )
flag = true;
}

public String runtime() {
return "On() execute times==" + times+" ID=="+id;
}

//
public void createANewThread() {
new Thread(new Runnable() {
public void run() {
while (true)
try {
Thread.sleep(1);
// 1ms
on();
} catch (Exception e) {}
}
}).start();
}

@Override
public void run() {
createANewThread();
//
while (true)
offAndCount();
}

public static void main(String[] args) {
Test t = new Test();
try {
Thread.sleep(3000);
} catch (Exception e) {}
System.out.println(t.runtime());
System.exit(0);
}

}