码迷,mamicode.com
首页 > 编程语言 > 详细

多线程互斥,用到Synchronized

时间:2015-06-07 12:25:32      阅读:122      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!