在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java/ 使用 synchronized 想輸出 4,3,2,1,4,3,2,1 為什么無(wú)

使用 synchronized 想輸出 4,3,2,1,4,3,2,1 為什么無(wú)效,不報(bào)錯(cuò)

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() {

    }

    /*
    當(dāng)兩個(gè)并發(fā)線程 CurrentThread01 和 CurrentThread02 訪問(wèn)同一個(gè)對(duì)象 syncThread 中的 synchronized 代碼塊時(shí),
    在同一時(shí)刻只能有一個(gè)線程得到執(zhí)行,另一個(gè)線程受阻塞,必須等待當(dāng)前線程執(zhí)行完這個(gè)代碼塊以后才能執(zhí)行該代碼塊。

    CurrentThread01 和 CurrentThread02是互斥的,因?yàn)樵趫?zhí)行synchronized代碼塊時(shí)會(huì)鎖定當(dāng)前的對(duì)象,
    只有執(zhí)行完該代碼塊才能釋放該對(duì)象鎖,
    下一個(gè)線程才能執(zhí)行并鎖定該對(duì)象。
     */
    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) {

            }
        }
    }

}

回答
編輯回答
青黛色

你的thread創(chuàng)建了,但沒(méi)有啟動(dòng),要通過(guò)start啟動(dòng)
把main函數(shù)改成下面這樣就可以了

    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();

    }
2018年5月18日 12:17
編輯回答
命多硬
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() {

    }

    /*
    當(dāng)兩個(gè)并發(fā)線程 CurrentThread01 和 CurrentThread02 訪問(wèn)同一個(gè)對(duì)象 syncThread 中的 synchronized 代碼塊時(shí),
    在同一時(shí)刻只能有一個(gè)線程得到執(zhí)行,另一個(gè)線程受阻塞,必須等待當(dāng)前線程執(zhí)行完這個(gè)代碼塊以后才能執(zhí)行該代碼塊。

    CurrentThread01 和 CurrentThread02是互斥的,因?yàn)樵趫?zhí)行synchronized代碼塊時(shí)會(huì)鎖定當(dāng)前的對(duì)象,
    只有執(zhí)行完該代碼塊才能釋放該對(duì)象鎖,
    下一個(gè)線程才能執(zhí)行并鎖定該對(duì)象。
     */
    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) {

            }
        }
    }

}
2017年8月9日 18:13