/*
多线程 :
线程 : 程序中执行的顺序流
多线程 : 多任务执行,多路径执行
多任务之间可以实现同时执行
优点 : 提高效率,提高性能
缺点 :设计更复杂,可能会造成数据不安全
了解 : 三高网站 : 高可能 高性能 高并发
进程与线程的区别 :
进程 : 资源分配的最小单位,每个进程都具有自己的代码与数据空间(进程上下文),进程之间的切换资源消耗大
线程 : cpu调度与执行的最小单位,一个进程至少包含1~n个线程,同一个进程的多个线程共享这个进程的代码数据空间,每个线程都有自己的运行栈与程序计数器,线程的切换消耗小
多线程的学习内容 :
线程的创建 *****
线程的状态
线程的安全 *****
线程通信
*/
/*
线程创建方式 :
1.继承Thread类,重写run() + start()开启线程
2.实现Runnable接口,重写run() + start()开启线程 --> *****
优点 :
接口多实现,类只能单继承
实现资源共享
3.实现juc包下Callable接口,重写call() + 线程池 --> 了解
*/
public class Class002_Thread extends Thread {
/*
定义线程体
*/
@Override
public void run() {
for(int i =1;i<=20;i++){
System.out.println("一边喝水");
try {
Thread.sleep(1); //让出cpu资源
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//主线程
//创建线程
Class002_Thread th = new Class002_Thread();
//开启线程
th.start(); //告诉cpu,我们线程可以被调度
for(int i =1;i<=20;i++){
System.out.println("一边吃饭...");
try {
Thread.sleep(1); //让出cpu资源
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*
线程创建方式 :
2.实现Runnable接口,重写run() + start()开启线程 --> *****
优点 :
接口多实现,类只能单继承
实现资源共享
*/
public class Class003_Thread implements Runnable{
public static void main(String[] ar