最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA多線(xiàn)程Thread和Runnable的實(shí)現(xiàn)

 更新時(shí)間:2013年03月17日 12:11:01   作者:  
java中實(shí)現(xiàn)多線(xiàn)程有兩種方法:一種是繼承Thread類(lèi),另一種是實(shí)現(xiàn)Runnable接口。

java中只允許單一繼承,但允許實(shí)現(xiàn)多個(gè)接口,因此第二種方法更靈活。

復(fù)制代碼 代碼如下:

/**
     * 運(yùn)行繼承java.lang.Thread類(lèi)定義的線(xiàn)程
     */
    public void startOne() {
        // 創(chuàng)建實(shí)例
        OneThread oneThread = new OneThread();
        // 啟動(dòng)線(xiàn)程ThreadA
        oneThread.startThreadA();
        try {
            // 設(shè)置線(xiàn)程休眠1秒
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 停止線(xiàn)程,此處為什么不用stop()方法,因?yàn)樵摲椒ㄒ呀?jīng)廢棄,但可以用在死鎖。
        oneThread.stopThreadA();
    }

復(fù)制代碼 代碼如下:

/**
     * 運(yùn)行實(shí)現(xiàn)Runnable接口定義的線(xiàn)程
     */
    public void startTwo() {
        // 創(chuàng)建實(shí)例
        Runnable runnable = new TwoThread();
        // 將實(shí)例放入到線(xiàn)程中
        Thread threadB = new Thread(runnable);
        // 啟動(dòng)線(xiàn)程
        threadB.start();
    }

復(fù)制代碼 代碼如下:

// 繼承Thread類(lèi)定義線(xiàn)程
class OneThread extends Thread {
    private boolean running = false;

    public void start() {
        this.running = true;
        super.start();
    }
    public void run() {
        int i = 0;
        try {
            while (running) {
                System.out.println("繼承Thread類(lèi)定義線(xiàn)程程序體......" + i++);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void startThreadA() {
        System.out.println("啟動(dòng)繼承Thread類(lèi)定義線(xiàn)程");
        this.start();
    }
    public void stopThreadA() {
        System.out.println("關(guān)閉繼承Thread類(lèi)定義線(xiàn)程");
        this.running = false;
    }
}

復(fù)制代碼 代碼如下:

// 實(shí)現(xiàn)Runnable接口定義線(xiàn)程
class TwoThread implements Runnable {
    private Date runDate;

    public void run() {
        System.out.println("實(shí)現(xiàn)Runnable接口定義線(xiàn)程程序體......");
        this.runDate = new Date();
        System.out.println("線(xiàn)程啟動(dòng)時(shí)間......" + runDate);
    }

復(fù)制代碼 代碼如下:

public static void main(String[] args) {
        // 實(shí)例化對(duì)象
        ThreadStartAndStop threadStartAndStop = new ThreadStartAndStop();
        threadStartAndStop.startOne();
        threadStartAndStop.startTwo();
    }

 啟動(dòng)繼承Thread類(lèi)定義線(xiàn)程
繼承Thread類(lèi)定義線(xiàn)程程序體......0
繼承Thread類(lèi)定義線(xiàn)程程序體......1
繼承Thread類(lèi)定義線(xiàn)程程序體......2
繼承Thread類(lèi)定義線(xiàn)程程序體......3
繼承Thread類(lèi)定義線(xiàn)程程序體......4
關(guān)閉繼承Thread類(lèi)定義線(xiàn)程
實(shí)現(xiàn)Runnable接口定義線(xiàn)程程序體......
線(xiàn)程啟動(dòng)時(shí)間......Fri Mar 15 12:56:57 CST 2013

相關(guān)文章

最新評(píng)論

湖南省| 子洲县| 凤冈县| 拜城县| 蕉岭县| 衡阳市| 长治县| 浦北县| 平安县| 黑龙江省| 三门县| 荥经县| 临沭县| 邻水| 新和县| 容城县| 博客| 如东县| 高青县| 望谟县| 双柏县| 滦平县| 日土县| 航空| 离岛区| 姚安县| 吉水县| 金山区| 东乌珠穆沁旗| 平乡县| 威远县| 吴江市| 新龙县| 西平县| 高邑县| 三门峡市| 宁化县| 岳普湖县| 巫溪县| 安丘市| 沙河市|