Java8多線程的啟動方法__Java

來源:互聯網
上載者:User

一、進程與線程的區別
總:作業系統執行多任務,每個任務就是進程;進程執行多任務,每個任務就是線程;內含項目關聯性
分:
1.調度性:在引入線程的OS中,把線程作為調度和指派的基本單位,把進程作為資源擁有的基本單位
2.並發性:在引入線程的OS中,不僅進程間可以並發執行,一個進程的多個線程之間也可以並發執行
3.擁有資源:進程是擁有資源的基本單位,線程除了擁有一點運行中必不可少的資源(線程式控制制塊,程式計數器,一組寄存器值和堆棧)外,本身基本沒有系統資源,但是可以訪問進程的資源。
4.建立或撤銷進程時系統都要為它分配或回收資源,進程切換所要保留的現場資訊也多於線程。由於一個進程的線程間共用同一地址空間和開啟檔案,因此它們的同步與通訊更加容易。
二、多線程的啟動方法
1、繼承Thread:重寫run()方法,調用start()啟動線程

public class TestThread extends Thread{    @Override    public void run()    {        //寫入具體方法        //getName()是執行個體方法,返回當前線程名        System.out.println(getName());          //currentThread()是類方法,返回正在執行的線程對象        Thread.currentThread();             }    public static void main()    {        TestThread thread = new TestThread();        thread.start();    }}

2、實現Runnable介面建立線程類

public class TestRunnable implements Runnable{    public void run()    {        //寫入具體方法    }    public static void main()    {        //1.以介面實現對象和線程名作為建構函式的參數        TestRunnable runnable = new TestRunnable();        Thread thread1 = new Thread(runnable,"thread1");        thread1.start();        //2.Lambda運算式建立線程        Thread thread2 = new Thread(()->{            //寫入具體方法        },"thread2");        thread2.start();        //3.匿名類建立線程        Thread thread3 = new Thread(new Runnable()        {            @Override            public void run()            {                //寫入具體方法            }        },"thread3");        thread3.start();    }}

Tip:設定Android Studio支援Lambda運算式的方法
http://blog.csdn.net/nicolelili1/article/details/52275263
3、使用Callable和Future建立線程
用法與Runnable大同小異,Callable介面提供call()方法,支援傳回值並可以拋出異常;Future介面代表Callable介面裡call()的傳回值。Lambda運算式方法不再舉例

public class TestCallableFuture{    public static void main()    {        FutureTask<Integer> task = new FutureTask<Integer>(            new Callable<Integer>()            {                @Override                public Integer call() throws Exception                {                    return null;                }            }        );        new Thread(task,"有傳回值的線程").start();        try        {            /**             * get方法擷取call()的傳回值,該方法造成程式阻塞,等子線程執行結束獲得傳回值             * 可以傳入等待時間(時間長度,時間單位),超出時間拋出TimeoutException異常             */            System.out.println(task.get().toString());            System.out.println(task.get(1000,TimeUnit.MILLISECONDS).toString());        }        catch (Exception e)        {            e.printStackTrace();        }    }}

4.小結:使用2、3方法可以使多個線程共用一個target對象,推薦

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.