1.多线程有什么作用(为什么需要多线程)
- 单核 CPU 的发展遇到了瓶颈. 要想提⾼算⼒, 就需要多核 CPU. ⽽并发编程能更充分利⽤多核 CPU 资源.
- 有些任务场景需要 "等待 IO", 为了让等待 IO 的时间能够去做⼀些其他的⼯作(这样可以大大提高工作效率), 也需要⽤到并发编程
- 其次, 虽然多进程也能实现并发编程, 但是线程⽐进程更轻量(后面会解释原因**)**
- 创建线程⽐创建进程更快
- 销毁线程⽐销毁进程更快
- 调度线程⽐调度进程更快
2.线程和进程之间的区别和联系
- 进程是系统资源分配的基本单位,包括CPU 内存 硬盘 网络带宽等重要的资源。如果频繁的创建销毁进程,就会频繁的进行资源的申请和内存的释放,就会产生大量的开销。
- 线程又被称作是“轻量级进程”,⼀个线程就是⼀个 "执⾏流". 每个线程之间都可以按照顺序执⾏⾃⼰的代码. 多个线程之间 "同时" 执⾏ 着多份代码.
- 进程是包含线程的. 每个进程⾄少有⼀个线程存在,即主线程
- 进程和进程之间不共享内存空间. 同⼀个进程的线程之间共享同⼀个内存空间
- 进程是系统分配资源的最⼩单位,线程是系统调度的最⼩单位
- ⼀个进程挂了⼀般不会影响到其他进程. 但是⼀个线程挂了, 可能把同进程内的其他线程⼀起带⾛(整 个进程崩溃,因为该进程可能在多处都需要调用挂了的那个线程)
- 以吃鸡蛋为例
- 此时的进程是一个人吃100个鸡蛋,速度是很慢的(还会被撑死)
- 此时在开一个进程,两个进程同时进行吃鸡蛋的工作(但创建两个进程开销比较大,且效率不是很高)
- 此时引入多线程,更多的人来吃鸡蛋,此时效率就会很高。
- 但是线程并不是越多越好,cpu的资源是有限的,当线程多了线程之间就会对cpu资源进行竞争(很多人在一张桌子上面吃鸡蛋,离桌子远的的人就会拿不到鸡蛋,就会发生争抢)
3.如何创建多线程
- 上面运用吃鸡蛋的例子说明了多线程的好处,接下来介绍创建多线程的几种方法。
1.继承Thread重写run方法
- 获取线程的引用 Thread.currentThread()
package classTest1;
class myThread extends Thread {
public void run (){
while (true) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
Thread t = new myThread();
t.start();
while (true) {
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//多线程,进入死循环
//打印的结果是hello main 比hello thread快一点
//由于调用线程需要开销,调用start创建线程是需要时间,所以慢
//但是顺序并不是绝对的,因为线程是类似于“随机调度”,一个线程什么时候被调度到cpu上执行的时机是不确定的
2.实现Runnable接口重写run
package classTest2;
public class myThread implements Runnable{
@Override
public void run() {
System.out.println("hhh");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package classTest2;
public class Test {
public static void main(String[] args) throws InterruptedException {
/*
* 用类的类型创建对象的过程称为类的实例化
* myThread实现了runnable接口
* Runnable就相当于属于 myThread这个类型
* 类:共同特征的描述*/
Runnable r = new myThread();
//还是需要Thread才能创建出真正的线程
Thread t = new Thread(r);
t.start();
while (true) {
System.out.println("ggggg");
Thread.sleep(1000);
}
}
}
3.继承Thread,重写run,但是使用匿名内部类
package classTest3;
public class Test {
public static void main(String[] args) {
/*
* 使用匿名内部类
* 匿名 没有名字,意味着只可以使用一次
*使用该格式创建匿名内部类 ({}中定义的就是匿名内部类)
* 相当于是Thread的一个子类 */
Thread t = new Thread() {
@Override
public void run() {
while (true) {
System.out.println("lll");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
while(true) {
System.out.println("gggg");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
4.实现Runnable,重写run匿名内部类
package classTest4;
public class Test {
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
/*
* 填写了Runnable打的匿名内部类的实例
* 与classTest2相似
* */
@Override
public void run() {
while (true ) {
System.out.println("ggg");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
while (true) {
System.out.println("hhhhh");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
5.使用lambda表达式(较为方便的格式)
package classTest5;
import java.awt.*;
public class Test {
public static void main(String[] args) {
/*
* lambda表达式的写法
* */
Thread t = new Thread(() -> {
while (true) {
System.out.println("ggg");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
while (true) {
System.out.println("hhhh");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
总结
- 线程进程的区别
- 线程的优势(多线程编程)
- 注意线程调度的先后顺序是不确定的!!!