文章目录
- 进程和线程
-
- 创建线程的五种写法
- 继承Thread,重写run
- 实现Runnable(接口),重写run
- 继承Thread,重写run,但是使用匿名内部类
- 实现Runnable(接口),重写run,但是使用匿名内部类
- 使用lambda表达式
- 请说明Thread类中run和start的区别
进程和线程
进程
- 进程:是正在执行的程序,是资源分配的基本单位,具有独立的地址空间
- 操作系统会为其分配CPU和内存
线程
- 线程:引入线程是为了解决进程开销大,浪费资源的情景,并且多进程并发效率比较低
- 线程是调度执行的基本单位
- 线程之间会相互影响,一个线程挂了,会影响到整个进程都异常结束,线程也自然会结束
进程和线程的区别
- 进程包含线程,一个进程里面有多个线程或者是一个线程
- 进程和线程都是用来实现并发编程场景的,但是线程比进程更轻量和高效
- 同一个进程的线程之间共用同一份资源(内存和硬盘),省去了申请资源的开销
- 进程和进程之间都是独立存在的,不会相互影响,同一个进程中,线程和线程之间会相互影响(线程安全问题 + 线程出现异常)
- 进程是分配资源的基本单位,线程是调度执行的基本单位
创建线程的五种写法
继承Thread,重写run
package Thread;class MyThread extends Thread{public void run(){while(true) {System.out.println("hello Thread!");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
public class Demo1 {public static void main(String[] args) throws InterruptedException {Thread thread = new MyThread();thread.start();while(true){System.out.println("hello main!");Thread.sleep(1000);}}
}
实现Runnable(接口),重写run
package Thread;class MyRunable implements Runnable{public void run(){while(true){System.out.println("hello thread!");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
public class Demo2 {public static void main(String[] args) throws InterruptedException {Runnable myRunable = new MyRunable();Thread thread = new Thread(myRunable);thread.start();while(true){System.out.println("hello main!");Thread.sleep(1000);}}
}
继承Thread,重写run,但是使用匿名内部类
- 使用匿名内部类的方式创建出线程
package Thread;public class Demo3 {public static void main(String[] args) {Thread thread = new Thread(){public void run(){while(true){System.out.println("hello Thread!");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}};thread.start();while(true){System.out.println("hello main!");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
实现Runnable(接口),重写run,但是使用匿名内部类
package Thread;public class Demo4 {public static void main(String[] args) {Runnable runnable = new Runnable(){public void run(){System.out.println("hello Thread!");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}};Thread thread = new Thread(new Runnable(){public void run(){System.out.println("hello Thread!");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});thread.start();while(true){System.out.println("hello main!");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
使用lambda表达式
- lambda表达式相当于是匿名内部类的替换写法
package Thread;public class Demo5 {public static void main(String[] args) {Thread thread = new Thread(()->{while(true){System.out.println("hello Thread!");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});thread.start();while(true){System.out.println("hello main!");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
请说明Thread类中run和start的区别
- 从方法的区别,及运行结果的区别分别说明
1. start方法可以用来启动一个新的线程,run方法只是一个普通的方法,在主线程中执行
2.start方法只能调用一次,run方法可以调用多次
3.调用start方法会执行新的线程,新线程和主线程并发执行,run方法只是线程的入口,start方法调用了系统API,start方法创建出了线程,让线程再调用run方法
4.run方法和主线程同步执行,start方法启动的线程和主线程异步执行
5.run方法按顺序执行,start方法调用的线程中的代码执行顺序由线程调度器决定,顺序不确定