最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1. 創(chuàng)建線程   

1.1 通過構(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. 停止線程,

千萬不用stop(),stop會立即終止線程。

通過interrupt()中斷線程,但是中斷并沒有停止線程,配合異常來實(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的話,可能會導(dǎo)致interrupt被捕獲,而繞過if(this.interrupted())的判斷而無法終止線程。

6. 等待和通知        

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

public final void wait() throws InterruptedException

線程通知:

public final native void notify()

注意:在notify()方法后,當(dāng)前線程不會馬上釋放該對象鎖,要等到執(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)先級

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

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;

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

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

8. 守護(hù)線程

類似守護(hù)進(jìn)程,Java存在兩種線程:用戶線程和守護(hù)線程。它是一種特殊線程,執(zhí)行的是一種后臺服務(wù),當(dāng)一個系統(tǒng)中不存在非守護(hù)線程的時候,守護(hù)線程會自己銷毀。典型的守護(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ù)線程時,JVM會退出。

 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,且沒有die。

當(dāng)isAlive的話就會拋出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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

白城市| 叶城县| 南通市| 潮州市| 石嘴山市| 苏尼特左旗| 桐城市| 宁海县| 射阳县| 巍山| 静海县| 江华| 北流市| 张家港市| 大洼县| 达日县| 桑日县| 铜山县| 惠水县| 新巴尔虎左旗| 和平区| 西昌市| 平顺县| 土默特左旗| 巍山| 辽宁省| 赤壁市| 桦南县| 成武县| 黑水县| 崇义县| 蓝山县| 温泉县| 侯马市| 晋城| 利辛县| 古田县| 金华市| 兴化市| 辽宁省| 唐海县|