标签:
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();
			 
		}
		
	}
}
标签:
原文地址:http://www.cnblogs.com/pplwb/p/4558202.html