同时有多个线程进入了同步方法

J2EE 码拜 7年前 (2017-04-21) 1239次浏览
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无缓冲区

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明同时有多个线程进入了同步方法
喜欢 (0)
[1034331897@qq.com]
分享 (0)