溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

線程創(chuàng)建的方式是什么

發(fā)布時(shí)間:2021-10-09 15:52:05 來(lái)源:億速云 閱讀:127 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容介紹了“線程創(chuàng)建的方式是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

Thread 、Runnable、Callable三種方式

線程創(chuàng)建的方式是什么

創(chuàng)建一個(gè)新的執(zhí)行線程有兩種方法。 一個(gè)是將一個(gè)類聲明為Thread的子類。 這個(gè)子類應(yīng)該重寫run類的方法Thread 。 然后可以分配并啟動(dòng)子類的實(shí)例。 例如,計(jì)算大于規(guī)定值的素?cái)?shù)的線程可以寫成如下:

線程創(chuàng)建的方式是什么

package com.example.thread.class1;// 實(shí)現(xiàn)多線程的第一種方法,繼承 Thread 類,重寫 run ,調(diào)用 start 方法開啟線程public class TestThread1 extends Thread{@Override    public void run() {// 線程方法體        for (int i = 0; i < 2000; i++) {
            System.out.println("我在看代碼****"+i);        }
    }public static void main(String[] args) {//創(chuàng)建一個(gè)線程對(duì)象        TestThread1 thread1 = new TestThread1();        thread1.start();        // 主線程        for (int i = 0; i < 2000; i++) {
            System.out.println("我在學(xué)習(xí)多線程****"+i);        }
    }
}

線程創(chuàng)建的方式是什么

線程開啟不一定立即執(zhí)行,由cpu調(diào)度

實(shí)現(xiàn)多線程下載圖片

package com.example.thread.class1;import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;// 實(shí)現(xiàn)多線程同步下載圖片 2public class TestThread2 extends Thread {

    String url; // 網(wǎng)絡(luò)圖片地址    String name; // 保存文件名     public TestThread2(String url,String name){this.url =url;        this.name =name;     }@Override    public void run() {
        downLoader loader = new downLoader();        try {
            loader.downLoad(url,name);            System.out.println("下載的圖片"+name);        } catch (MalformedURLException e) {
            e.printStackTrace();        }
    }public static void main(String[] args) {
        TestThread2 thread1 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","1");        TestThread2 thread2 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","2");        TestThread2 thread3 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","3");        thread1.start();        thread2.start();        thread3.start();    }
}// 下載器 WebdownLoaderclass downLoader{public  void downLoad(String url,String name) throws MalformedURLException {try {
            FileUtils.copyURLToFile(new URL(url),new File(name));        } catch (IOException e) {
            e.printStackTrace();            System.out.println("IO異常,downloader 方法出現(xiàn)問題");        }
    }
}

另一種方法來(lái)創(chuàng)建一個(gè)線程是聲明實(shí)現(xiàn)類Runnable接口。 那個(gè)類然后實(shí)現(xiàn)了run方法。 然后可以分配類的實(shí)例,在創(chuàng)建Thread時(shí)作為參數(shù)傳遞,并啟動(dòng)。 這種其他風(fēng)格的同一個(gè)例子如下所示:

線程創(chuàng)建的方式是什么

package com.example.thread.class1;import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;// 實(shí)現(xiàn)多線程同步下載圖片 2public class TestThread2 implements Runnable {

    String url; // 網(wǎng)絡(luò)圖片地址    String name; // 保存文件名     public TestThread2(String url,String name){this.url =url;        this.name =name;     }@Override    public void run() {
        downLoader loader = new downLoader();        try {
            loader.downLoad(url,name);            System.out.println("下載的圖片"+name);        } catch (MalformedURLException e) {
            e.printStackTrace();        }
    }public static void main(String[] args) {
        TestThread2 thread1 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","1");        TestThread2 thread2 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","2");        TestThread2 thread3 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","3");        new Thread(thread1).start();        new Thread(thread2).start();        new Thread(thread3).start();    }
}// 下載器 WebdownLoaderclass downLoader{public  void downLoad(String url,String name) throws MalformedURLException {try {
            FileUtils.copyURLToFile(new URL(url),new File(name));        } catch (IOException e) {
            e.printStackTrace();            System.out.println("IO異常,downloader 方法出現(xiàn)問題");        }
    }
}

線程創(chuàng)建的方式是什么

多線程中的靜態(tài)代理模式

1.真實(shí)角色(委托對(duì)象)

2.代理角色

3.共同實(shí)現(xiàn)的接口

package com.example.demo.class1;import java.lang.reflect.WildcardType;/** * 靜態(tài)代理模式: * 真實(shí)對(duì)象和代理對(duì)象都要同時(shí)實(shí)現(xiàn)同一個(gè)接口 * * 好處: * 代理對(duì)象可以做很多真實(shí)對(duì)象做不了的事情 * 真實(shí)對(duì)象就專注自己的事情 */public class StacticProxy {    public static void main(String[] args) {//        new Thread( ()->System.out.println("我愛你")).start();        new WeddingCompany(new You()).HappyMarry();        /*WeddingCompany weddingCompany = new WeddingCompany(new You());        weddingCompany.HappyMarry();*/    }}interface Marry{    void HappyMarry();}// 真實(shí)角色,你去結(jié)婚class You implements Marry{    @Override    public void HappyMarry() {        System.out.println("陳老師要結(jié)婚了.....");    }}// 代理角色,幫助你結(jié)婚class WeddingCompany implements  Marry{    private  Marry target; // 需要代理的對(duì)象,真實(shí)的對(duì)象    public WeddingCompany(Marry target) {        this.target = target;    }    @Override    public void HappyMarry() {        before();        this.target.HappyMarry(); // 這就是真實(shí)對(duì)象的結(jié)婚        after();    }    private void after() {        System.out.println("結(jié)婚后收錢...");    }    private void before() {        System.out.println("結(jié)婚前布置現(xiàn)場(chǎng)...");    }}

“線程創(chuàng)建的方式是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI