java多進程-創建和啓動

Java多線程創建

  • java.lang.Thread
    – 線程繼承Thread類,實現run方法
  • java.lang.Runnable接口
    – 線程實現Runnable接口,實現run方法

Java多線程啓動

  • 啓動
    – 調用start方法,會自動以新進程調用run方法
    – 直接調用run方法,將變成串行執行
    – 同一個線程,多次start會報錯,只執行第一次start方法
    – 多個線程啓動,其啓動的先後順序是隨機的
    – 線程無需關閉,只有其run方法執行結束後,自動關閉
    – main函數(主線程)可能早於新線程結束,整個程序並不終止
    – 整個程序終止是等所有的線程都終止(包括main函數線程)
    JNI,Java Native Interface,它提供了若干個API,可以使得Java程序調用C/C++程序

Thread 與 Runnable實現對比

  • 因爲一個類只能繼承一個父類,但可以繼承多個接口,所以建議使用Runnable接口,這樣會節省父類的名額
  • Thread類實現Runnable
  • Runnable啓動時需要Thread了的支持
  • Runnable更容易實現多線程中資源共享
  • Thread裏面,必須用static變量,才能實現變量共享,在Runnable裏面可以使用普通的變量就可以實現變量共享
  • 結論:建議實現Runnable接口來完成多線程
    Java的四個主要接口:Clonable,用於對象克隆; Comparable,用於對象比較;Serializable,用於對象序列化;Runnable,用於對象線程化

實例一:run\start方法的區別

1 調用run方法,來啓動run方法,將會是串行運行
2 調用start方法,來啓動run方法,將會是並行運行(多線程運行)

package com.torey;

/**
 * @ClassName:ThreadDemo0
 * @Description:
 * @author: Torey
 */
public class ThreadDemo0 {
    public static void main(String[] args) throws InterruptedException {
        /*
        * 1 調用run方法,來啓動run方法,將會是串行運行
        * 2 調用start方法,來啓動run方法,將會是並行運行(多線程運行)
        * */
        TestThread1 testThread1=new TestThread1();
        new Thread(testThread1).start();
        new Thread(testThread1).run();
        while (true){
            System.out.println("main thread is running");
            Thread.sleep(1000);
        }
    }
}
class TestThread1 implements Runnable{
    public void run() {
        while (true){
            System.out.println("TestThread1 is running"+Thread.currentThread().getId());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

實例二:mian線程和子線程都結束了,整個程序纔算終止

1 main線程可能早於子線程結束
2 mian線程和子線程都結束了,整個程序纔算終止。

package com.torey;

/**
 * @ClassName:ThreadDemo2
 * @Description:
 * @author: Torey
 */
public class ThreadDemo2 {
    /*
    1 main線程可能早於子線程結束
    2 mian線程和子線程都結束了,整個程序纔算終止。
     */
    public static void main(String[] args) throws InterruptedException {
        new TestThread2().start();
        System.out.println("主線程結束");
    }
}
class TestThread2 extends Thread{
    @Override
    public void run(){
        while (true) {
            System.out.println("TestThread2");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

一個線程不能多次start,多次start將報異常;

package com.torey;

/**
 * @ClassName:ThreadDemo4
 * @Description:
 * @author: Torey
 */
public class ThreadDemo4 {
    public static void main(String[] args){
        /*
        1 一個線程不能多次start,多次start將報異常;
        2 多個線程對象都start後,哪一個先執行,完全由JVM/操作系統來主導,程序員無法指定
         */
        TestThread4 t = new TestThread4();
        t.start();
        // 這裏會報錯,再次執行會報錯
        t.start();
        // 
        TestThread4 t1 = new TestThread4();
        t1.start();
        TestThread4 t2 = new TestThread4();
        t2.start();
    }
}
class TestThread4 extends Thread{
    @Override
    public void run() {
        System.out.println("TestThread4 run,id=" + Thread.currentThread().getId());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章