一、線程創建方法(5種)
1.繼承Thread類
class MyThread extends Thread {@Overridepublic void run() {System.out.println("MyThread is running");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}
這里中間運行的代碼,需要拋出異常。
然后創建MyThread的實例,并執行
public class Demo1 {public static void main(String[] args) throws InterruptedException {Thread t = new MyThread();t.start();//t.run();while(true){System.out.println("hello world");Thread.sleep(1000);}}
}
2.實現Runnable接口
class MyRunnable implements Runnable {@Overridepublic void run() {while (true) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
然后創建Thread實例,將Runnable對象作為參數。
public class Demon2 {public static void main(String[] args) throws InterruptedException {Runnable runnable = new MyRunnable();Thread thread = new Thread(runnable);thread.start();while(true) {System.out.println("hello main");Thread.sleep(1000);}}
}
這里也可以寫成
public class Demon2 {public static void main(String[] args) throws InterruptedException {
/* Runnable runnable = new MyRunnable();Thread thread = new Thread(runnable);*/Thread thread = new Thread(new MyRunnable());thread.start();while(true) {System.out.println("hello main");Thread.sleep(1000);}}
}
后面的使用方法不變。
3.使用匿名內部類創建Thread子類對象
public class Demon2 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(){@Overridepublic void run() {while(true) {System.out.println("hello world");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}};t.start();while(true) {System.out.println("hello my");Thread.sleep(1000);}}
}
實際上就是第一種方法的變形。
4.匿名內部類創建Runnable子類對象
public class Demon3 {public static void main(String[] args) throws InterruptedException {Runnable runnable = new Runnable() {@Overridepublic void run() {while(true) {System.out.println("hello world");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}};Thread thread = new Thread(runnable);thread.start();while(true) {System.out.println("hello my");Thread.sleep(1000);}}
}
后面依舊需要創建Thread實例化,將Runnable作為對象參數傳入。
5.使用lambda表達式創建子類
public class Demon4 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{while(true) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});t.start();while(true) {System.out.println("hello my");Thread.sleep(1000);}}
}
這也是最簡單最常用的方法。
二、線程中斷
這里采用Thread中的interrupted方法
public class Demon10 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{while(!Thread.currentThread().isInterrupted()){//System.out.println("t:"+Thread.currentThread().getName());System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {//throw new RuntimeException(e);break;}}System.out.println("t結束");});t.start();//System.out.println("main:"+Thread.currentThread().getName());Thread.sleep(3000);System.out.println("main嘗試終止");t.interrupt();}
}
t.interrupt在調用后內部類中的while循環條件被打破,直接打印“t結束”。