package ThreadTest.Test1; public class Tests implements Runnable { public synchronized void syn() { System.out.println(Thread.currentThread().getName() + "进到了同步方法里"); for (int i = 0; i < 10; i++) { System.err.println("线程" + Thread.currentThread().getName() + "输出" + i); } } public void run() { syn(); } public static void main(String[] args) { Tests s = new Tests(); Thread t1 = new Thread(s); Thread t2 = new Thread(s); Thread t3 = new Thread(s); Thread t4 = new Thread(s); Thread t5 = new Thread(s); Thread t6 = new Thread(s); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); t6.start(); } }
解决方案
20
System.out.println有缓冲区
System.err.println无缓冲区
System.err.println无缓冲区