public synchronized void out2(String name) {
int length = name.length();
for (int i = 0; i < length; i++) {
System.out.print(name.charAt(i));
}
System.out.println();
}
方法二
(2) 第二种 在方法里面加synchronized块
public void out(String name) {
int length = name.length();
synchronized (Outputer.class) {
for (int i = 0; i < length; i++) {
System.out.print(name.charAt(i));
}
System.out.println();
}
}
方法三
(3)第三种 java.util.concurrent.locks.Lock
public void output(String name){
int len = name.length();
lock.lock();
try{
for(int i=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}finally{
lock.unlock();
}
}