package thead.safe.safe3;

public class Run {
    public static void main(String[] args) {
        PrivateNum privateNum1 = new PrivateNum();
        PrivateNum privateNum2 = new PrivateNum();

        new ThreadA(privateNum1).start();
        new ThreadB(privateNum2).start();
    }

    /**
     * 在这个方法中前面是否加 synchronized 对结果都没有影响，因为资源不是共享的
     */
    public static class PrivateNum {
        synchronized public void testMethod() {
            try {
                System.out.println(Thread.currentThread().getName() + " 开始 " + System.currentTimeMillis());
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName() + " 结束 " + System.currentTimeMillis());
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    }

    // 线程A
    public static class ThreadA extends Thread {

        private final PrivateNum privateNum;

        public ThreadA(PrivateNum privateNum) {
            super();
            this.privateNum = privateNum;
        }

        @Override
        public void run() {
            super.run();
            privateNum.testMethod();
        }
    }

    // 线程B
    public static class ThreadB extends Thread {

        private final PrivateNum privateNum;

        public ThreadB(PrivateNum privateNum) {
            super();
            this.privateNum = privateNum;
        }

        @Override
        public void run() {
            super.run();
            privateNum.testMethod();
        }
    }
}
