天天看點

Java 多線程實作的兩種方法

目錄

  • ​​實作多線程,有兩種方法​​
  • ​​線程優先級​​

實作多線程,有兩種方法

  1. 繼承thread,重寫run方法,【本質也是實作runnable接口的一個執行個體】(比較拿捏)
  2. 建立一個類,implements runnable接口【這個好處多,建議用這個】

方法1:

package xiancheng.MyThread;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 從南到北
 * @Date: 11/29/2021/11:10
 * @Description:
 *      線程拿捏的方法:
 *
 */
public class Mythread extends Thread {
    @Override
    public void run() {
        for(int i=0;i<100;i++){
            System.out.println(i);
        }
    }

    public static void main(String[] args) {
        Mythread m1 = new Mythread();
        Mythread m2 = new Mythread();
        
//        這樣不會啟動線程,隻是單純的調用線程而已。
//        m1.run();
//        m2.run();

        m1.start(); //start ,使線程開始執行,java虛拟機調用此線程的run方法
        m2.start();
    }
}      
Java 多線程實作的兩種方法

方法2:

實作Runnable接口

  1. 定義一個類MyRunnable實作Runnable接口
  2. 在MyRunnable類中重寫run()方法
  3. 建立MyRunnable類的對象建立Thread類的對象,把MyRunnable對象作為構造方法的參數
  4. start啟動線程
package dch.thread05;
//通過實作接口的方式來實作多線程,不影響他再繼承其他的類
public class MyRunnable implements Runnable {
//實作runnable接口,并沒有繼承thread類,好處:将來可以有自己的父類,
    @Override
    public void run() {
        for (int i=0;i<100;i++){
            //這個方法會報錯,因為這個類隻是實作了Runnable接口,和Thread類沒有關系,
            //不能直接調用Thread類中的方法
//            System.out.println(getName()+":"+i);
            //我們先拿到目前線程,然後再去拿這個名字,就可以了
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}      
package dch.thread05;
/*
    方式2:實作Runnable接口
        1:定義一個類MyRunnable實作Runnable接口
        2:在MyRunnable類中重寫run()方法
        3:建立MyRunnable類的對象
        4:建立Thread類的對象,把MyRunnable對象作為構造方法的參數
        5:啟動線程
 */

public class MyRunnableDemo {
    public static void main(String[] args) {
        //建立myrunable類對象

        MyRunnable my=new MyRunnable();
        //将這個看成,同一個資源,由多個線程去使用

        //建立thread類對象,把myrunnable對象作為構造方法的參數
        //Thread(Runnable target)
//        Thread t1=new Thread(my);
//        Thread t2=new Thread(my);
        //   Thread(Runnable target, String name)
        //将myRunnable實作類的對象作為參數傳遞,可以将myRunnable看作同一個資源,由多個線程去使用
        Thread t1=new Thread(my,"高鐵"); 
        Thread t2=new Thread(my,"飛機");

    }
}      

MyRunnable my=new MyRunnable();

//将這個看成,同一個資源,由多個線程去使用

線程優先級

package xiancheng.MyThread;

public class ThreadPriority extends Thread {
    @Override
    public void run() {
        for (int i = 0;i<100;i++){
            System.out.println(getName()+":"+i);
        }
    }
}      
package xiancheng.MyThread;

/*
    Thread類中設定和擷取線程優先級的方法
        public final void setPriority(int newPriority):更改此線程的優先級
        public final int getPriority():傳回此線程的優先級
 */
public class ThreadPriorityDemo {
    public static void main(String[] args) {
        ThreadPriority tp1 = new ThreadPriority();
        ThreadPriority tp2 = new ThreadPriority();
        ThreadPriority tp3 = new ThreadPriority();

        tp1.setName("高鐵");
        tp2.setName("飛機");
        tp3.setName("汽車");

        //public final int getPriority():傳回此線程的優先級
        System.out.println(tp1.getPriority()); //5
        System.out.println(tp2.getPriority()); //5
        System.out.println(tp3.getPriority()); //5

        //public final void setPriority(int newPriority):更改此線程的優先級
//        tp1.setPriority(10000); //IllegalArgumentException
//        System.out.println(Thread.MAX_PRIORITY); //10
//        System.out.println(Thread.MIN_PRIORITY); //1
//        System.out.println(Thread.NORM_PRIORITY); //5

        //設定正确的優先級,線程優先級高,隻能表示擷取cpu時間片的幾率的高,并不是每一次都會跑在最前面
        tp1.setPriority(5);
        tp2.setPriority(10);
        tp3.setPriority(1);

        tp1.start();
        tp2.start();
        tp3.start();
    }
}