传统线程的互斥技术:
关键字:Synchronized
例子:
public class TraditionalThreadSynchronized {
public static void main(String[] args) {
new TraditionalThreadSynchronized().init();
}
private void init(){
final Output output = new Output();
new Thread(new Runnable(){
@.....
public void run(){
while(true){
try(){
Thread.sleep(100);
}catch(){
e.printStrackTrace();
}
output.outputer("zhangsan");
}
}
}).start();
new Thread(new Runnable(){
@.....
public void run(){
while(true){
try(){
Thread.sleep(100);
}catch(){
e.printStrackTrace();
}
output.outputer("zhangsan");
}
}
}).start();
}
class Output{
public void outputer(String name){
int len = name.length();
synchronized(this){
for(int i = 0; i< len ; i ++){
System.out.println(name.charAt(i));
}
}
Syso();
}
}
}
//注:静态方法的锁是当前类的字节码。
原文:http://www.cnblogs.com/hujiandesblogs/p/7096567.html