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

Java多線程Thread基礎(chǔ)學(xué)習(xí)

 更新時(shí)間:2023年04月21日 09:23:42   作者:lmrylll  
每一個(gè)正在執(zhí)行的程序都是一個(gè)進(jìn)程,資源只有一塊,所以在同一時(shí)間段會(huì)有多個(gè)程序同時(shí)執(zhí)行,但是在一個(gè)時(shí)間點(diǎn)上,只能由一個(gè)程序執(zhí)行,多線程是在一個(gè)進(jìn)程的基礎(chǔ)之上的進(jìn)一步劃分,需要的朋友可以參考下

1. 創(chuàng)建線程   

1.1 通過(guò)構(gòu)造函數(shù):public Thread(Runnable target, String name){}  或:public Thread(Runnable target){}

示例:

Thread thread1 = new Thread(new MyThread(), "mythread");
class MyThread extends Thread(){
        public void run(){
             System.out.println("My First Thread');
        }
}

1.2 直接實(shí)現(xiàn)Runnable接口:

示例:

Thread thread2 = new Thread(new Runnable{}{
         public void run(){
                  System.out.println("This is my thread.");
          }
});

2. 運(yùn)行線程   

thead1.start()    

 3. sleep

try{
    #休眠1000ms
    Thread.sleep(1000);
}catch(InterruptedException e){
    e.printStackTrace();
}

4. getName() 獲取線程名字, getId()獲取線程id

System.out.println(Thread.currentThread().getName() + ":"+ Thread.currentThread().getId);

5. 停止線程,

千萬(wàn)不用stop(),stop會(huì)立即終止線程。

通過(guò)interrupt()中斷線程,但是中斷并沒(méi)有停止線程,配合異常來(lái)實(shí)現(xiàn):

public class Main {
   public static void main(String[] args) throws InterruptedException {
      try{
          Thread thread1=new Thread(new TheThread(),"thread1");
          thread1.start();
          Thread.sleep(2000);
          thread1.interrupt();
      }catch (InterruptedException e){
                  e.printStackTrace();
       }
   }
}
   class TheThread extends Thread{
     public void run() {
        super.run();
        for (int i = 0; i < 10; i++) {
           if(this.interrupted()){
              break;
        }
        System.out.println(Thread.currentThread().getName() + ":" + i);
     }
   }
}

注意,如果在TheThread類里加入catch InterruptException的話,可能會(huì)導(dǎo)致interrupt被捕獲,而繞過(guò)if(this.interrupted())的判斷而無(wú)法終止線程。

6. 等待和通知        

線程等待:當(dāng)前線程就處于等待狀態(tài),直到其他線程調(diào)用了notify()方法,線程才會(huì)繼續(xù)執(zhí)行

public final void wait() throws InterruptedException

線程通知:

public final native void notify()

注意:在notify()方法后,當(dāng)前線程不會(huì)馬上釋放該對(duì)象鎖,要等到執(zhí)行notify()方法的線程將程序執(zhí)行完,也就是退出同步代碼塊中。

 package wait.notify;
 
 public class ThreadWaitNotifyTest {
     final static Object object=new Object();
     public static class T1 extends Thread{
         public void run(){
             System.out.println(System.currentTimeMillis()+": T1 start");
             synchronized (object){
                 try {
                     System.out.println(System.currentTimeMillis()+": T1 wait");
                     object.wait();
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             }
             System.out.println(System.currentTimeMillis()+": T1 end");
         }
     }
     public static class T2 extends Thread{
         public void run(){
             System.out.println(System.currentTimeMillis()+": T2 start");
             synchronized (object){
                 System.out.println("T2 synchonized code start.");
                 object.notify();
                 try {
                     Thread.sleep(2000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }finally{
                     System.out.println("T2 synchonized code end.");
                 }
 
             }
             try {
                 Thread.sleep(2000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             System.out.println(System.currentTimeMillis()+": T2 end");
         }
     }
     public static void main(String[] args){
         Thread thread1=new T1();
         Thread thread2=new T2();
         thread1.start();
         thread2.start();
     }
 }

輸出結(jié)果:

7. 線程優(yōu)先級(jí)

高優(yōu)先級(jí)的線程將會(huì)獲得更多的CPU資源。一共分為10個(gè)優(yōu)先級(jí)。

public final void setPriority(int newPriority)

源碼分析:

public final void setPriority(int newPriority) {
        ThreadGroup g;
        checkAccess();
        if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
            throw new IllegalArgumentException();
        }
        if((g = getThreadGroup()) != null) {
            if (newPriority > g.getMaxPriority()) {
                newPriority = g.getMaxPriority();
            }
            setPriority0(priority = newPriority);
        }
}
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;

可見(jiàn)線程最高優(yōu)先級(jí)為10, 最低為1, 默認(rèn)為5.

當(dāng)設(shè)定的newPriority高于該線程組ThreadGroup的最高Priority時(shí),只能分配該線程組的最高Priority

8. 守護(hù)線程

類似守護(hù)進(jìn)程,Java存在兩種線程:用戶線程和守護(hù)線程。它是一種特殊線程,執(zhí)行的是一種后臺(tái)服務(wù),當(dāng)一個(gè)系統(tǒng)中不存在非守護(hù)線程的時(shí)候,守護(hù)線程會(huì)自己銷毀。典型的守護(hù)線程:JVM的垃圾回收線程。

public final void setDaemon(boolean on)

示例:

public class Main {
    public static void main(String[] args) throws InterruptedException {
       TheThread theThread=new TheThread();
        theThread.setDaemon(true);//設(shè)置守護(hù)線程
        theThread.start();
        Thread.sleep(5000);
        System.out.println("全都退出啦");
    }
    public static class TheThread extends Thread{
        public void run(){
            int i = 0;
            while (true){
                i++;
                System.out.println(Thread.currentThread().getId()+":"+i);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

源碼分析:

設(shè)置線程為用戶線程(user thread)或守護(hù)線程(daemon thread),當(dāng)剩余運(yùn)行的線程均為守護(hù)線程時(shí),JVM會(huì)退出。

 public final void setDaemon(boolean on) {
        checkAccess();
        if (isAlive()) {
            throw new IllegalThreadStateException();
        }
        daemon = on;
    }

其中checkAccesss()方法如下:

 public final void checkAccess() {
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkAccess(this);
        }
    }

該方法用于判斷當(dāng)前運(yùn)行的線程是否有修改此線程的權(quán)限。

而public final native boolean isAlive();用于判斷該線程是否處于alive狀態(tài),即該線程是否已經(jīng)start,且沒(méi)有die。

當(dāng)isAlive的話就會(huì)拋出IllegalThreadStateException異常。

所以,設(shè)置守護(hù)線程的方法,邏輯就是先判斷當(dāng)前線程是否有修改的權(quán)限,再判斷是否處于alive狀態(tài),如果不處于alive狀態(tài),則根據(jù)boolean變量on的值更改它的狀態(tài),即true:設(shè)為daemon線程,false:設(shè)為user線程。

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

相關(guān)文章

  • Springboot中@scheduled注解解析

    Springboot中@scheduled注解解析

    這篇文章主要介紹了Springboot中@scheduled注解解析,定時(shí)任務(wù)就是在指定時(shí)間執(zhí)行程序,或周期性執(zhí)行計(jì)劃任務(wù),Java中實(shí)現(xiàn)定時(shí)任務(wù)的方法有很多,本文從從JDK自帶的一些方法來(lái)實(shí)現(xiàn)定時(shí)任務(wù)的需求,需要的朋友可以參考下
    2023-09-09
  • 看完這篇文章獲得一些java if優(yōu)化技巧

    看完這篇文章獲得一些java if優(yōu)化技巧

    if 是每個(gè)語(yǔ)言都有的語(yǔ)法,也是最基礎(chǔ)的語(yǔ)法。因?yàn)榇a本來(lái)就很晦澀,所以才有了程序員這個(gè)中間件,今天就聊一下我的一些關(guān)于 if 思路和總結(jié)
    2021-07-07
  • Maven插件的安裝及使用

    Maven插件的安裝及使用

    這篇文章主要介紹了Maven插件的安裝及使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Spring事務(wù)處理原理步驟詳解

    Spring事務(wù)處理原理步驟詳解

    這篇文章主要介紹了Spring事務(wù)處理原理步驟詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • JAVA異常體系結(jié)構(gòu)詳解

    JAVA異常體系結(jié)構(gòu)詳解

    Java把異常當(dāng)作對(duì)象來(lái)處理,并定義一個(gè)基類java.lang.Throwable作為所有異常的超類,下面通過(guò)本文給大家分享JAVA異常體系結(jié)構(gòu),感興趣的朋友一起看看吧
    2017-11-11
  • pagehelper插件顯示total為-1或1的問(wèn)題

    pagehelper插件顯示total為-1或1的問(wèn)題

    這篇文章主要介紹了pagehelper插件顯示total為-1或1,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java中的Lambda表達(dá)式詳解

    Java中的Lambda表達(dá)式詳解

    這篇文章主要介紹了Java中的Lambda表達(dá)式詳解,Lambda 表達(dá)式是 JDK8 的一個(gè)新特性,可以取代大部分的匿名內(nèi)部類,寫出更優(yōu)雅的 Java 代碼,尤其在集合的遍歷和其他集合操作中,可以極大地優(yōu)化代碼結(jié)構(gòu),需要的朋友可以參考下
    2024-01-01
  • Spring純注解開發(fā)模式讓開發(fā)簡(jiǎn)化更簡(jiǎn)化

    Spring純注解開發(fā)模式讓開發(fā)簡(jiǎn)化更簡(jiǎn)化

    Spring3.0引入了純注解開發(fā)的模式,框架的誕生是為了簡(jiǎn)化開發(fā),那注解開發(fā)就是簡(jiǎn)化再簡(jiǎn)化。Spring的特性在整合MyBatis方面體現(xiàn)的淋漓盡致哦
    2022-08-08
  • Java多線程中的CountDownLatch解析

    Java多線程中的CountDownLatch解析

    這篇文章主要介紹了Java多線程中的CountDownLatch解析,CountDownLatch是一個(gè)阻塞部分線程直到其他線程執(zhí)行完成后喚醒的同步計(jì)數(shù)器,核心是其內(nèi)部類Sync繼承于AQS,同時(shí)也是利用的AQS的同步原理,也稱之為閉鎖,需要的朋友可以參考下
    2023-11-11
  • Java?Lambda表達(dá)式語(yǔ)法及用法示例

    Java?Lambda表達(dá)式語(yǔ)法及用法示例

    這篇文章主要給大家介紹了關(guān)于Java?Lambda表達(dá)式語(yǔ)法及用法的相關(guān)資料,lambda表達(dá)式是JAVA8中提供的一種新的特性,它支持Java也能進(jìn)行簡(jiǎn)單的"函數(shù)式編程",文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10

最新評(píng)論