Java學習筆記44(多線程一),java學習筆記

來源:互聯網
上載者:User

Java學習筆記44(多線程一),java學習筆記

多線程的概念:略

多線程的目的:提高效率

 

主線程:

package demo;//主線程public class Demo {    public static void main(String[] args) {        function();        System.out.println(1);    }        public static void function(){        for (int i = 0; i < 10000; i++) {            System.out.println(i);        }    }}

這段簡單的代碼,我們發現:

必須要先執行方法輸出完10000次的數字後才可以列印第二行的數字1

那麼有沒有方法,可以做到在執行方法的同時執行第二行的輸出?

 

Thread類

建立新線程的兩種方法:

第一種:

package demo;public class SubThread extends Thread {    //重寫run方法    public void run(){        for(int i = 0 ; i< 50 ; i++){            System.out.println(i+"run");        }    }}
package demo;public class ThreadDemo {    public static void main(String[] args) {        SubThread st1 = new SubThread();        st1.start();        for (int i = 0; i < 50; i++) {            System.out.println(i+"main");        }    }}

這裡輸出時候,發現列印的run和main隨機出現,交錯出現,而不像以前那樣按順序列印

原因:建立了新的線程,兩條線程由cpu選擇執行,我們無法控制

 

start方法開啟新的線程,繼承了Thread類因為只有繼承了它才可以操作線程

重寫run方法因為,Thread類本身沒有寫入有意義的run方法,相當於一個模板,供開發人員使用

 

線程名:

每個線程都有自己的名字,主線程名:main,其他建立線程預設名:Thread-n

擷取、修改線程名:

 

package demo1;public class NameThread extends Thread {    public void run(){        System.out.println(super.getName());        //輸出:預設是Thread-0,如果修改了,就是hello    }}
package demo1;public class ThreadDemo {    public static void main(String[] args) {        NameThread nt1 = new NameThread();        nt1.setName("hello");        //修改線程名為hello,主線程不能改名        nt1.start();                Thread t = Thread.currentThread();        System.out.println(t.getName());        //輸出:main    }}

 

Thread類的一個實用方法:

package demo1;public class ThreadDemo {    public static void main(String[] args) throws InterruptedException {        for (int i = 0; i < 10; i++) {            Thread.sleep(1000);            System.out.println(i);        }        //每次列印都會等待一秒,參數是毫秒值    }}

 

 

第二種建立線程方法:

package demo1;public class SubRunnable implements Runnable {    public void run() {        for (int i = 0; i < 50; i++) {            System.out.println(i + "run");        }    }}
package demo1;public class ThreadDemo {    public static void main(String[] args) {        SubRunnable sr1 = new SubRunnable();        Thread t1 = new Thread(sr1);        t1.start();        for (int i = 0; i < 50; i++) {            System.out.println(i + "main");        }    }}

 

這種方式好處:

1.介面可以多實現,避免了單繼承的局限性

2.線程和方法分離,更符合物件導向的特點

3.資源實現共用

 

這兩種方式可以實用匿名內部類實現:

package demo1;public class ThreadDemo {    public static void main(String[] args) {        // 繼承方式        new Thread() {            public void run() {                System.out.println("1");            }        }.start();        // 實現介面方式        new Thread(new Runnable() {            public void run() {                System.out.println(2);            }        }).start();    }}

 

線程的狀態:

1.建立狀態:new Thread()建立線程對象

2.運行狀態:使用了start()方法進入運行狀態

3.退出狀態:run方法結束,或者調用了stop方法(已淘汰,不建議實用)

4.阻塞狀態:有時候使用了start方法,但不一定立即運行,或者運行之後CPU由於一些原因不再分配,由運行狀態轉到阻塞狀態

5.休眠狀態:前邊提到的sleep方法就是這種狀態,也有可能轉到阻塞狀態或者運行狀態

6.等待狀態:wait方法,無限等待,notify方法可以喚醒線程,可能轉到運行或阻塞狀態

注意:受阻塞是等待CPU的資源,休眠等待是放棄CPU的執行

 

線程池的概念:

 一個容器,存入多個線程,需要時候,拿出執行,

運行結束後線程再回到容器中,這種方式可以提高效率

實現線程池:

package demo1;public class ThreadPoolRunnable implements Runnable {    public void run(){        System.out.println(Thread.currentThread().getName()+"線程提交任務");        //輸出: pool-1-thread-1線程提交任務        //        pool-1-thread-2線程提交任務    }
package demo1;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ThreadPoolDemo {    public static void main(String[] args) {        //調用工廠類的方法建立線程池        ExecutorService es1 = Executors.newFixedThreadPool(2);        es1.submit(new ThreadPoolRunnable());        es1.submit(new ThreadPoolRunnable());        //運行後不會停                es1.shutdown();//銷毀線程池,不常用    }}

 

實現線程的Callable介面方式:

它彌補了Runnable方式的缺陷:無法拋出異常,並且有傳回值

使用方法和Runnable方式基本一致:

樣本:

package demo1;import java.util.concurrent.Callable;public class ThreadPoolCallable implements Callable<String>{    public String call(){        return "a";    }}
package demo1;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class ThreadPoolDemo {    public static void main(String[] args) throws InterruptedException, ExecutionException {        ExecutorService es1 = Executors.newFixedThreadPool(2);        Future<String>f1 = es1.submit(new ThreadPoolCallable());        String s1 = f1.get();        System.out.println(s1);        //得到傳回值,輸出a    }}

 

 

簡單應用:多線程非同步計算

使用兩個線程計算求和:

package demo1;import java.util.concurrent.Callable;public class GetSumCallable implements Callable<Integer> {    private int a;    public GetSumCallable(int a) {        this.a = a;    }    public Integer call() {        int sum = 0;        for (int i = 0; i <= a; i++) {            sum += i;        }        return sum;    }}
package demo1;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class ThreadPoolDemo {    public static void main(String[] args) throws Exception {        ExecutorService es1 = Executors.newFixedThreadPool(2);        Future<Integer> f1 = es1.submit(new GetSumCallable(100));        Future<Integer> f2 = es1.submit(new GetSumCallable(300));        System.out.println(f1.get());        System.out.println(f2.get());        es1.shutdown();    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.