package com;
public class TraditionalThreadSynchronizeTest {
    public static void main(String[] args) {
        new TraditionalThreadSynchronizeTest().init();
    }
    public void init() {
        final Output output = new Output();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    output.output("zhangxiaoxiang");
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    output.output2("lihuoming");
                }
            }
        }) .start();
                   
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    output.output3("lihuoming");
                }
            }
        }) .start();
    }
    static class Output {
        public void output(String name) {
            int len = name.length();
            synchronized (Output.class) {
            //synchronized (this) {
                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 (Output.class) {
            //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();
//          synchronized (Output.class) {
            //synchronized (this) {
                for (int i = 0; i < len; i++) {
                System.out.print(name.charAt(i));
            //}
            System.out.println();
            }
        }
    }
}

代码量提升之路 - 线程同步与互斥_public