package Demo3;
public class TraditionalThreadSynchronized {
public static void main(String args[]){
TraditionalThreadSynchronized ts=new TraditionalThreadSynchronized();
ts.init();
}
public void init(){
final output out =new output();
new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.getname1("xiaozhang");
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//new output().getname1("gaoda");
out.getname1("gaoda");
}
}
}).start();
}
static class output{
public void getname1(String name){
//synchronized (output.class) {
synchronized (this) {
int len =name.length();
for(int i=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
public synchronized void getname2(String name){
int len =name.length();
for(int i=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
public static synchronized void getname3(String name){
int len =name.length();
for(int i=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
}
public class TraditionalThreadSynchronized {
public static void main(String args[]){
TraditionalThreadSynchronized ts=new TraditionalThreadSynchronized();
ts.init();
}
public void init(){
final output out =new output();
new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.getname1("xiaozhang");
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//new output().getname1("gaoda");
out.getname1("gaoda");
}
}
}).start();
}
static class output{
public void getname1(String name){
//synchronized (output.class) {
synchronized (this) {
int len =name.length();
for(int i=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
public synchronized void getname2(String name){
int len =name.length();
for(int i=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
public static synchronized void getname3(String name){
int len =name.length();
for(int i=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
}