一、简介
进程:
进程是操作系统资源分配的基本单位, 进程是指正在运行的程序实例。每个进程都有自己的内存空间、代码、数据和资源。操作系统通过管理进程来控制计算机的资源分配。每个进程都有一个唯一的标识符,称为进程 ID,以便操作系统可以识别和管理它。进程可以与其他进程通信,并且可以启动其他进程以执行不同的任务。。
多进程:
在操作系统中可以同时运行多个程序。
线程:
是操作系统进行运算调度的基本单位,它被包含在进程之中,是进程中的实际运作单位,一条线程指的是进程中一个单一顺序的控制流。一个进程可以并发多个线程,每条线程并行执行不同的任务。
多线程:
在一个应用程序中可以同时执行多个功能,例如迅雷中可以同时执行多个下载任务旧是多线程。
特性:
1、一个进程可以包含多个线程。
2、一个线程必须属于某个线程,进程是线程的容器。
3、一个进程中的多个线程共享该进程中的所有资源。
CPU时间片:
CPU时间片是计算机中操作系统调度程序所使用的一种时间分配机制。它是指操作系统将CPU轮流分配给各个进程或线程执行一定时间的长度,每个时间长度被称为一个时间片。在时间片内,进程或线程可以执行自己的任务,并在时间片结束后交出CPU,等待下一轮调度。通过使用CPU时间片,操作系统可以使多个进程或线程并发执行,从而提高系统的效率。同时,由于操作系统可以在时间片结束后及时切换到下一个进程或线程,避免了某个进程或线程长时间占用CPU而导致其他进程或线程无法执行的情况。
1、从表面上看各个程序是同时运行的,实际上CPU在同一时间只能执行一个程序。
2、只是CPU在很短的时间内,在不同程序间切换。轮流执行每个程序,执行速度很快,所以感觉上像是同时在运行。
二、创建线程
五种方式:
1、继承Thread类
2、实现Runnable接口
3、继承Thread类 使用匿名内部类
4、实现Runnable接口,使用匿名内部类
5、使用lambda表达式
1、继承Thread类
步骤:
1、定义一个类,继承自Thread类,重写run()方法。
2、创建该类的实例,即创建一个线程。
3、调用start()方法,启动线程()(不能直接调用run()方法)。
//继承Thread类
class MyThread extends Thread{
@Override
public void run() {
while (true){
System.out.println("hello t");
}
}
}
public class ThreadDome1 {
public static void main(String[] args) {
Thread t = new MyThread();
t.start();
while (true){
System.out.println("hello main");
}
}
}
2、实现Runnable接口
步骤:1、定义一个类,实现Runnable接口,实现run()方法。
2、创建实现类的实例。
3、创建Thread类的一个实例,将上一步中实现类的实例传入
4、调用start()方法,启动线程
//实现Runnable接口
class MyRunnable implements Runnable{
@Override
public void run() {
while (true) {
System.out.println("hello t");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadDome2 {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread t = new Thread(myRunnable);
t.start();
while (true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3、继承Thread类 使用匿名内部类
public class ThreadDome3 {
public static void main(String[] args) {
Thread thread = new Thread(){
@Override
public void run() {
while (true){
System.out.println("hello t");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.start();
while (true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
4、实现Runnable接口,使用匿名内部类
public class ThreadDome4 {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("hello t");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
while (true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
5、使用lambda表达式
public class ThreadDome5 {
public static void main(String[] args) {
Thread t = new Thread( () ->{
while (true){
System.out.println("hello t");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} );
t.start();
System.out.println(t.getName());
while (true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3、线程和进程的关系以及区别
进程和线程的关系:
(1)一个线程只能属于一个进程,而一个进程可以有多个线程,但至少有一个线程。 (2)资源分配给进程,同一进程的所有线程共享该进程的所有资源。 (3)处理机分给线程,即真正在处理机上运行的是线程。 (4)线程在执行过程中,需要协作同步。不同进程的线程间要利用消息通信的办法实现同步。线程是指进程内的一个执行单元,也是进程内的可调度实体.
进程与线程的区别:
(1)调度:线程作为调度和分配的基本单位,进程作为拥有资源的基本单位 (2)并发性:不仅进程之间可以并发执行,同一个进程的多个线程之间也可并发执行 (3)拥有资源:进程是拥有资源的一个独立单位,线程不拥有系统资源,但可以访问隶属于进程的资源. (4)系统开销:在创建或撤消进程时,由于系统都要为之分配和回收资源,导致系统的开销明显大于创建或撤消线程时的开销。