欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java多線程基礎(chǔ)

 更新時(shí)間:2021年10月27日 14:55:48   作者:冬日毛毛雨  
這篇文章主要介紹Java多線程基礎(chǔ),線程是進(jìn)程的一個(gè)實(shí)體,是CPU調(diào)度和分派的基本單位,它是比進(jìn)程更小的能獨(dú)立運(yùn)行的基本單位,多線程指在單個(gè)程序中可以同時(shí)運(yùn)行多個(gè)不同的線程執(zhí)行不同的任務(wù),下面來學(xué)習(xí)具體的詳細(xì)內(nèi)容

一、線程

什么是線程:

線程是進(jìn)程的一個(gè)實(shí)體,是CPU調(diào)度和分派的基本單位,它是比進(jìn)程更小的能獨(dú)立運(yùn)行的基本單位。

什么是多線程:

多線程指在單個(gè)程序中可以同時(shí)運(yùn)行多個(gè)不同的線程執(zhí)行不同的任務(wù)。

二、創(chuàng)建多線程的方式

多線程的創(chuàng)建方式有三種:ThreadRunnable、Callable

1、繼承Thread類實(shí)現(xiàn)多線程

      Thread thread = new Thread() {
            @Override
            public void run() {
                super.run();
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("1:" + Thread.currentThread().getName());
                    System.out.println("2:" + this.getName());
                }
            }
        };
        thread.start();

2、實(shí)現(xiàn)Runnable接口方式實(shí)現(xiàn)多線程

Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("3:" + Thread.currentThread().getName());
                }
            }
        });
        thread1.start();

3、Callable接口創(chuàng)建線程

public class CallableTest {

    public static void main(String[] args) {
        System.out.println("當(dāng)前線程是:" + Thread.currentThread());
        Callable myCallable = new Callable() {
            @Override
            public Integer call() throws Exception {
                int i = 0;
                for (; i < 10; i++) {

                }
                //當(dāng)前線程
                System.out.println("當(dāng)前線程是:" + Thread.currentThread()
                        + ":" + i);
                return i;
            }
        };
        FutureTask<Integer> fu = new FutureTask<Integer>(myCallable);
        Thread th = new Thread(fu, "callable thread");

        th.start();

        //得到返回值
        try {
            System.out.println("返回值是:" + fu.get());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

當(dāng)前線程是:Thread[main,5,main]
當(dāng)前線程是:Thread[callable thread,5,main]:10
返回值是:10

總結(jié):

實(shí)現(xiàn)Runnable接口相比繼承Thread類有如下優(yōu)勢(shì):

  • 可以避免由于Java的單繼承特性而帶來的局限;
  • 增強(qiáng)程序的健壯性,代碼能夠被多個(gè)線程共享,代碼與數(shù)據(jù)是獨(dú)立的;
  • 適合多個(gè)相同程序代碼的線程區(qū)處理同一資源的情況。

實(shí)現(xiàn)Runnable接口和實(shí)現(xiàn)Callable接口的區(qū)別:

  • Runnable是自從java1.1就有了,而Callable是1.5之后才加上去的
  • Callable規(guī)定的方法是call() , Runnable規(guī)定的方法是run()
  • Callable的任務(wù)執(zhí)行后可返回值,而Runnable的任務(wù)是不能返回值是(void)
  • call方法可以拋出異常,run方法不可以
  • 運(yùn)行Callable任務(wù)可以拿到一個(gè)Future對(duì)象,表示異步計(jì)算的結(jié)果。它提供了檢查計(jì)算是否完成的方法,以等待計(jì)算的完成,并檢索計(jì)算的結(jié)果。通過Future對(duì)象可以了解任務(wù)執(zhí)行情況,可取消任務(wù)的執(zhí)行,還可獲取執(zhí)行結(jié)果。
  • 加入線程池運(yùn)行,Runnable使用ExecutorServiceexecute方法,Callable使用submit方法。

三、線程的生命周期與狀態(tài)

四、線程的執(zhí)行順序

Join線程的運(yùn)行順序

原理:

1、定時(shí)器

import java.util.Timer;
import java.util.TimerTask;

public class TraditionalTimerTest {
    public static void main(String[] args) {

//         new Timer().schedule(new TimerTask() {
//             @Override
//             public void run() {
//
//                 System.out.println("bombing!");
//             }
//         },10000);

        class MyTimberTask extends TimerTask {
            @Override
            public void run() {

                System.out.println("bombing!");
                new Timer().schedule(new MyTimberTask(), 2000);
            }
        }


        new Timer().schedule(new MyTimberTask(), 2000);


        int count = 0;
        while (true) {
            System.out.println(count++);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

0
1
bombing!
2
3
bombing!
4
5
bombing!
6
省略...

2、線程的互斥與同步通信

public class TraditionalThreadSynchronized {

    public static void main(String[] args) {

        new TraditionalThreadSynchronized().init();
    }

    private void init() {
        final Outputer outputer = new Outputer();

        new Thread(new Runnable() {
            @Override
            public void run() {

                while (true) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    outputer.output("kpioneer");

                }
            }
        }).start();

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//                while (true) {
//                    try {
//                        Thread.sleep(10);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    outputer.output2("Tom");
//
//                }
//            }
//        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {

                while (true) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    outputer.output3("Jack");

                }
            }
        }).start();
    }

  static   class Outputer {

        public void output(String name) {

            int len = name.length();

            synchronized (Outputer.class) {

                for (int i = 0; i < len; i++) {
                    System.out.print(name.charAt(i));
                }
                System.out.println();
            }

        }

        public synchronized void output2(String name) {
            int len = name.length();

            for (int i = 0; i < len; i++) {
                System.out.print(name.charAt(i));
            }
            System.out.println();
        }

      public static synchronized void output3(String name) {
          int len = name.length();

          for (int i = 0; i < len; i++) {
              System.out.print(name.charAt(i));
          }
          System.out.println();
      }
    }
}

3、線程同步通信技術(shù)

子線程循環(huán)10次,接著主線程循環(huán)100,接著又回到子線程循環(huán)10次,接著再回到主線程有循環(huán)100,如此循環(huán)50次,請(qǐng)寫出程序。

public class TraditionalThreadCommunication {
    public static void main(String[] args) {

        final Business business = new Business();
        new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 1; i <= 50; i++) {
                            business.sub(i);
                        }
                    }
                }
        ).start();
        for (int i = 1; i <= 50; i++) {
            business.main(i);
        }
    }

}

/**
 *要用到共同數(shù)據(jù)(包括同步鎖)的若干方法應(yīng)該歸在同一個(gè)類身上,
 * 這種設(shè)計(jì)正好體現(xiàn)了高類聚和和程序的健壯性
 */
class Business {
    private boolean bShouldSub = true;
    public synchronized void sub(int i) {
        if(!bShouldSub) {

            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int j = 1; j <= 10; j++) {
            System.out.println("sub thread sequece of " + j + ",loop of " + i);

        }
        bShouldSub = false;
        this.notify();
    }

    public synchronized void main(int i) {
        if(bShouldSub) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int j = 1; j <=100; j++) {
            System.out.println("main thread sequece of " + j + ",loop of " + i);

        }
        bShouldSub = true;
        this.notify();
    }
}

sub thread sequece of 1,loop of 1
sub thread sequece of 2,loop of 1
sub thread sequece of 3,loop of 1
sub thread sequece of 4,loop of 1
sub thread sequece of 5,loop of 1
sub thread sequece of 6,loop of 1
sub thread sequece of 7,loop of 1
sub thread sequece of 8,loop of 1
sub thread sequece of 9,loop of 1
sub thread sequece of 10,loop of 1
main thread sequece of 1,loop of 1
main thread sequece of 2,loop of 1
main thread sequece of 3,loop of 1
main thread sequece of 4,loop of 1
main thread sequece of 5,loop of 1
main thread sequece of 6,loop of 1
main thread sequece of 7,loop of 1
main thread sequece of 8,loop of 1
main thread sequece of 9,loop of 1
main thread sequece of 10,loop of 1
main thread sequece of 11,loop of 1
省略中間...
main thread sequece of 99,loop of 1
main thread sequece of 100,loop of 1
sub thread sequece of 1,loop of 2
sub thread sequece of 2,loop of 2
sub thread sequece of 3,loop of 2
sub thread sequece of 4,loop of 2
sub thread sequece of 5,loop of 2
sub thread sequece of 6,loop of 2
sub thread sequece of 7,loop of 2
sub thread sequece of 8,loop of 2
sub thread sequece of 9,loop of 2
sub thread sequece of 10,loop of 2
main thread sequece of 1,loop of 2
main thread sequece of 2,loop of 2
main thread sequece of 3,loop of 2
省略...

到此這篇關(guān)于Java多線程基礎(chǔ)的文章就介紹到這了,更多相關(guān)Java多線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java單鏈表的增刪改查與面試題詳解

    Java單鏈表的增刪改查與面試題詳解

    單鏈表是鏈表的其中一種基本結(jié)構(gòu)。一個(gè)最簡(jiǎn)單的結(jié)點(diǎn)結(jié)構(gòu)如圖所示,它是構(gòu)成單鏈表的基本結(jié)點(diǎn)結(jié)構(gòu)。在結(jié)點(diǎn)中數(shù)據(jù)域用來存儲(chǔ)數(shù)據(jù)元素,指針域用于指向下一個(gè)具有相同結(jié)構(gòu)的結(jié)點(diǎn)。 因?yàn)橹挥幸粋€(gè)指針結(jié)點(diǎn),稱為單鏈表
    2022-09-09
  • Scala中優(yōu)雅的處理Null問題

    Scala中優(yōu)雅的處理Null問題

    Spark 采用混合方式,大部分情況下使用 Option,但個(gè)別時(shí)候出于性能原因才使用了null。一個(gè)很好的習(xí)慣是當(dāng)有方法返回值可能為null的時(shí)候,使用Option來代替,本文給大家介紹Scala處理Null的知識(shí)詳解,一起看看吧
    2021-08-08
  • 淺談Spring中的循環(huán)依賴問題與解決方案

    淺談Spring中的循環(huán)依賴問題與解決方案

    這篇文章主要介紹了淺談Spring中的循環(huán)依賴問題與解決方案,循環(huán)依賴就是兩個(gè)或則兩個(gè)以上的bean互相持有對(duì)方,最終形成閉環(huán),比如A依賴于B,B依賴于C,C又依賴于A,需要的朋友可以參考下
    2023-12-12
  • 詳解關(guān)于springboot-actuator監(jiān)控的401無權(quán)限訪問

    詳解關(guān)于springboot-actuator監(jiān)控的401無權(quán)限訪問

    本篇文章主要介紹了詳解關(guān)于springboot-actuator監(jiān)控的401無權(quán)限訪問,非常具有實(shí)用價(jià)值,有興趣的可以了解一下
    2017-09-09
  • springboot中使用mybatisplus自帶插件實(shí)現(xiàn)分頁的示例代碼

    springboot中使用mybatisplus自帶插件實(shí)現(xiàn)分頁的示例代碼

    這篇文章主要介紹了springboot中使用mybatisplus自帶插件實(shí)現(xiàn)分頁,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • springboot框架阿里開源低代碼工具LowCodeEngine

    springboot框架阿里開源低代碼工具LowCodeEngine

    這篇文章主要為大家介紹了springboot框架阿里開源低代碼LowCodeEngine工具使用詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • 使用Spring boot + jQuery上傳文件(kotlin)功能實(shí)例詳解

    使用Spring boot + jQuery上傳文件(kotlin)功能實(shí)例詳解

    本文通過實(shí)例代碼給大家介紹了使用Spring boot + jQuery上傳文件(kotlin) 功能,需要的朋友可以參考下
    2017-07-07
  • Springboot整合Rabbitmq之Confirm和Return機(jī)制

    Springboot整合Rabbitmq之Confirm和Return機(jī)制

    這篇文章主要介紹了Springboot整合Rabbitmq之Confirm和Return詳解,本篇重點(diǎn)進(jìn)行Confirm?機(jī)制和Return?機(jī)制的實(shí)現(xiàn)和說明,通過實(shí)例代碼相結(jié)合給大家詳細(xì)介紹,對(duì)Springboot整合Rabbitmq相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-02-02
  • springboot集成shiro權(quán)限管理簡(jiǎn)單實(shí)現(xiàn)

    springboot集成shiro權(quán)限管理簡(jiǎn)單實(shí)現(xiàn)

    這篇文章主要介紹了springboot集成shiro權(quán)限管理簡(jiǎn)單實(shí)現(xiàn),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • Spring+Quartz實(shí)現(xiàn)動(dòng)態(tài)任務(wù)調(diào)度詳解

    Spring+Quartz實(shí)現(xiàn)動(dòng)態(tài)任務(wù)調(diào)度詳解

    這篇文章主要介紹了Spring+Quartz實(shí)現(xiàn)動(dòng)態(tài)任務(wù)調(diào)度詳解,最近經(jīng)?;趕pring?boot寫定時(shí)任務(wù),并且是使用注解的方式進(jìn)行實(shí)現(xiàn),分成的方便將自己的類注入spring容器,需要的朋友可以參考下
    2024-01-01

最新評(píng)論