Thread类的基本用法(上)
一、线程创建方法(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结束”。