使用 synchronized 想输出 4,3,2,1,4,3,2,1 为什么无效,不报错

小刘
  • 526
package com.mvc;

public class NetSchedulerTest {

    private final String collectorJobGroup = "collectorJobGroup";
    private final String collectorTriggerGroup = "collectorTriggerGroup";

    public enum JobType {
        START, STOP
    }

    public static void main(String[] args) {
        System.out.println("enum = " + JobType.START);
        final NetSchedulerTest myt2 = new NetSchedulerTest();
        Thread CurrentThread01 = new Thread(  new Runnable() {
            public void run() { myt2.CurrentThread01();  }
        }, "CurrentThread01"  );
        Thread CurrentThread02 = new Thread(  new Runnable() {
            public void run() { myt2.CurrentThread02();   }
        }, "CurrentThread02"  );

    }

    // synchronize
    public synchronized void synStart() {

    }

    /*
    当两个并发线程 CurrentThread01 和 CurrentThread02 访问同一个对象 syncThread 中的 synchronized 代码块时,
    在同一时刻只能有一个线程得到执行,另一个线程受阻塞,必须等待当前线程执行完这个代码块以后才能执行该代码块。

    CurrentThread01 和 CurrentThread02是互斥的,因为在执行synchronized代码块时会锁定当前的对象,
    只有执行完该代码块才能释放该对象锁,
    下一个线程才能执行并锁定该对象。
     */
    public void CurrentThread01()
    {
        synchronized(this)
        {
            int i = 5;
            while( i-- > 0)
            {
                System.out.println(Thread.currentThread().getName() + " : " + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {

                }
            }
        }
    }

    public synchronized void CurrentThread02() {
        int i = 5;
        while( i-- > 0)
        {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            }
            catch (InterruptedException ie) {

            }
        }
    }

}

回复
阅读 700
2 个回答
✓ 已被采纳

你的thread创建了,但没有启动,要通过start启动
把main函数改成下面这样就可以了

    public static void main(String[] args) {
        System.out.println("enum = " + JobType.START);
        final NetSchedulerTest myt2 = new NetSchedulerTest();
        new Thread(  new Runnable() {
            public void run() { myt2.CurrentThread01();  }
        }, "CurrentThread01"  ).start();
        new Thread(  new Runnable() {
            public void run() { myt2.CurrentThread02();   }
        }, "CurrentThread02"  ).start();

    }
package com.mvc;

public class NetSchedulerTest {

    private final String collectorJobGroup = "collectorJobGroup";
    private final String collectorTriggerGroup = "collectorTriggerGroup";

    public enum JobType {
        START, STOP
    }

    public static void main(String[] args) {
        System.out.println("enum = " + JobType.START);
        final NetSchedulerTest myt2 = new NetSchedulerTest();
        Thread CurrentThread01 = new Thread(  new Runnable() {
            public void run() { myt2.CurrentThread01();  }
        }, "CurrentThread01"  );
        Thread CurrentThread02 = new Thread(  new Runnable() {
            public void run() { myt2.CurrentThread02();   }
        }, "CurrentThread02"  );

        CurrentThread02.start();
        CurrentThread01.start();
    }

    // synchronize
    public synchronized void synStart() {

    }

    /*
    当两个并发线程 CurrentThread01 和 CurrentThread02 访问同一个对象 syncThread 中的 synchronized 代码块时,
    在同一时刻只能有一个线程得到执行,另一个线程受阻塞,必须等待当前线程执行完这个代码块以后才能执行该代码块。

    CurrentThread01 和 CurrentThread02是互斥的,因为在执行synchronized代码块时会锁定当前的对象,
    只有执行完该代码块才能释放该对象锁,
    下一个线程才能执行并锁定该对象。
     */
    public void CurrentThread01()
    {
        synchronized(this)
        {
            int i = 5;
            while( i-- > 0)
            {
                System.out.println(Thread.currentThread().getName() + " : " + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {

                }
            }
        }
    }

    public synchronized void CurrentThread02() {
        int i = 5;
        while( i-- > 0)
        {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            }
            catch (InterruptedException ie) {

            }
        }
    }

}
你知道吗?

宣传栏