多線程

Thread類(須有start調(diào)用run才可啟動線程)

public void start()
使該線程開始執(zhí)行;Java 虛擬機(jī)調(diào)用該線程的 run 方法。
結(jié)果是兩個線程并發(fā)地運(yùn)行;當(dāng)前線程(從調(diào)用返回給 start 方法)和另一個線程(執(zhí)行其 run 方法)。多次啟動一個線程是非法的。特別是當(dāng)線程已經(jīng)結(jié)束執(zhí)行后,不能再重新啟動。


class MyThread extends Thread{
    private String name;
    public MyThread(String name){
        this.name = name;
    }
    public void run(){
        for(int x=0;x<200;x++){
            System.out.println(name+"----->"+x);
        }
    }
}
public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyThread m1 = new MyThread("Java");
        MyThread m2 = new MyThread("Javb");
        MyThread m3 = new MyThread("Javc");
        m1.run();
        m2.run();
        m3.run();
    }
}

多線程執(zhí)行

class MyThread extends Thread{
    private String name;
    public MyThread(String name){
        this.name = name;
    }
    public void run(){
        for(int x=0;x<200;x++){
            System.out.println(name+"----->"+x);
        }
    }
}
public class Demo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyThread m1 = new MyThread("Java");
        MyThread m2 = new MyThread("Javb");
        MyThread m3 = new MyThread("Javc");
        m1.start();
        m2.start();
        m3.start();
    }
}

Runnable接口(由于單繼承局限)

public class Thread extends Object implements Runnable

class MyThread implements Runnable{
    private String name;
    public MyThread(String name){
        this.name = name;
    }
    public void run(){
        for(int x=0;x<200;x++){
            System.out.println(name+"----->"+x);
        }
    }
}
public class Demo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyThread m1 = new MyThread("Java");
        MyThread m2 = new MyThread("Javb");
        MyThread m3 = new MyThread("Javc");
        new Thread(m1).start();
        new Thread(m2).start();
        new Thread(m3).start();
    }
}

用Runnable實(shí)現(xiàn)主體類,用Thread啟動線程

Callable接口

暫無

線程的命名與取得

構(gòu)造方法:Thread(Runnable target, String name)
public static Thread currentThread()此方法取得當(dāng)前對象

package Demo;
//如果啟動前未設(shè)置名字,則會自動編號命名
class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println(Thread.currentThread().getName());
    }
}
public class CollableDemo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        new Thread(mt).start();
        new Thread(mt).start();
        new Thread(mt).start();
    }
}
------------------------------------
Thread-0
Thread-1
Thread-2

設(shè)置名字:public final void setName(String name)

package Demo;

class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println(Thread.currentThread().getName());
    }
}
public class CollableDemo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        new Thread(mt,"線程A").start();
        new Thread(mt).start();
        new Thread(mt,"線程B").start();
        new Thread(mt).start();
    }

}

取得名字:public final String getName()

package Demo;

class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println(Thread.currentThread().getName());
    }
}
public class CollableDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        new Thread(mt,"線程A").start();
        mt.run();
    }

}
--------------------------
main
線程A

主方法就是一個線程(main線程),在子方法上創(chuàng)建的線程為子線程;main只是進(jìn)程上的一個子線程。

線程的休眠

public static void sleep(long millis,int nanos) throws InterruptedException

package Demo;

class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int x=0;x<1000;x++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+",x ="+x);
        }
    }
}
public class CollableDemo {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        new Thread(mt,"線程A").start();
    }
}

如果設(shè)置了多個線程對象,則一起進(jìn)入run()方法

package Demo;

class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int x=0;x<1000;x++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+",x ="+x);
        }
    }
}
public class CollableDemo {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        new Thread(mt,"線程A").start();
        new Thread(mt,"線程B").start();
        new Thread(mt,"線程C").start();
        new Thread(mt,"線程D").start();
        new Thread(mt,"線程E").start();
    }
}
------------------------------------
線程B,x =0
線程A,x =0
線程E,x =0
線程D,x =0
線程C,x =0
線程C,x =1
線程A,x =1
線程B,x =1
線程E,x =1
線程D,x =1
線程E,x =2
線程C,x =2
線程A,x =2
線程D,x =2
線程B,x =2
線程E,x =3
線程A,x =3
線程C,x =3
線程D,x =3
線程B,x =3
線程D,x =4
線程E,x =4
線程C,x =4
線程A,x =4
線程B,x =4

線程優(yōu)先級

設(shè)置優(yōu)先級:public final void setPriority(int newPriority)
取得優(yōu)先級:public final int getPriority()
使用int返回,有三個優(yōu)先級
最高:public static final int MAX_PRIORITY 10
中等:public static final int NORM_PRIORITY 5
最低:public static final int MIN_PRIORITY 1

package Demo;

class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int x=0;x<20;x++){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+",x ="+x);
        }
    }
}
public class CollableDemo {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        Thread t1 = new Thread(mt,"線程A");
        Thread t2 = new Thread(mt,"線程B");
        Thread t3 = new Thread(mt,"線程C");
        t3.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();
        t3.start();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文主要講了java中多線程的使用方法、線程同步、線程數(shù)據(jù)傳遞、線程狀態(tài)及相應(yīng)的一些線程函數(shù)用法、概述等。 首先講...
    李欣陽閱讀 2,584評論 1 15
  • Java多線程學(xué)習(xí) [-] 一擴(kuò)展javalangThread類 二實(shí)現(xiàn)javalangRunnable接口 三T...
    影馳閱讀 3,105評論 1 18
  • 線程概述 線程與進(jìn)程 進(jìn)程 ?每個運(yùn)行中的任務(wù)(通常是程序)就是一個進(jìn)程。當(dāng)一個程序進(jìn)入內(nèi)存運(yùn)行時,即變成了一個進(jìn)...
    閩越布衣閱讀 1,098評論 1 7
  • 一、認(rèn)識多任務(wù)、多進(jìn)程、單線程、多線程 要認(rèn)識多線程就要從操作系統(tǒng)的原理說起。 以前古老的DOS操作系統(tǒng)(V 6....
    GT921閱讀 1,091評論 0 3
  • 感統(tǒng)失調(diào)這個詞在國內(nèi)用的不是很廣泛,甚至很多家長聞所未聞,沒關(guān)系,在了解什么叫感統(tǒng)失調(diào)后你不會陌生的…或許你的孩子...
    小橙汁成長記閱讀 1,254評論 0 2

友情鏈接更多精彩內(nèi)容