首页 > 编程语言 > 详细

多线程互斥,用到Synchronized

时间:2015-06-07 12:24:07      阅读:302      评论:0      收藏:0      [点我收藏+]

 

package com.test;

public class TraditionalThreadSynchronized {

public static void main(String [] arge){
new TraditionalThreadSynchronized().init();
}

private void init(){
final outPuter out =new outPuter();
new Thread(new Runnable() {

public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
out.output("zhangxiaoxiang");
}

}
}).start();

new Thread(new Runnable() {

public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
out.output3("liguoming");
}

}
}).start();
}
static class outPuter{

public void output(String name){
int len = name.length();
synchronized (outPuter.class) {
for(int i = 0 ;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}

public synchronized void output2(String name){
int len = name.length();
synchronized (this) {
for(int i = 0 ;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}

public static synchronized void output3(String name){
int len = name.length();

for(int i = 0 ;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();

}

}

}

多线程互斥,用到Synchronized

原文:http://www.cnblogs.com/pplwb/p/4558202.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!