文章目录
前言
上篇博客,大概说明了计算机的历史和进程这一概念,初步对计算机的组成和运行有一定了解。这篇博客将正式进入对多线程的学习,希望对大家有所帮助~ ~ ~
认识线程
当然不是,首先进程是包含线程的,每个进程都至少有一个线程存在,即主线程。其次,进程和进程之间不共享内存空间,但是同一个进程的线程之间共享一个内存空间
总结:
· 进程是系统资源分配的最小单位,线程是系统调度的最小单位
· ⼀个进程挂了⼀般不会影响到其他进程. 但是⼀个线程挂了,可能把同进程内的其他线程⼀起带⾛(整个进程崩溃)
为什么要有线程?
因为随着计算机被要求处理的计算越来越多且复杂,计算机不断发展尽力提高算力。众所周知,计算机的核心是CPU,但是单核CPU发展到了瓶颈,于是多核CPU就横空出世。为了更好的利用多核优势,有大佬就提出了 “ 并发 ” 概念。
目前," 并发编程 “成为” 刚需 ",从某种角度来讲,并发编程大大提高了计算机运行的效率。
对于并发的理解大家可以去看上篇博客有详细说明。
• 创建线程 ⽐ 创建进程 更快.
• 销毁线程 ⽐ 销毁进程 更快.
• 调度线程 ⽐ 调度进程 更快.
当然,也有更高效的方法,我们以后再说…
Java的线程 和 操作系统的线程的关系
操作系统内核实现了线程这样的机制,并且对⽤⼾层提供了⼀些 API 供⽤⼾使⽤
Java标准库中 Thread 类可以视为是对操作系统提供的 API 进⾏了进⼀步的抽象和封装。
简单理解,操作系统把它内部的一个方法拿出来当作接口,去给其他用户使用,而Java中就对于把这个接口拿过来使用这个方法,并且做了一些改变。
大家如果对 API 这个概念不清楚,把它当作一个接口就行,如果需要更专业的理解,可以看看这篇博客什么是API?
如何创建线程
继承 Thread 类创建线程
class MyThread extends Thread{
//相当于线程的入口
@Override
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) {
Thread t = new MyThread();
//真正在系统中创建出一个线程
t.start();//相当于多了一个执行流,可以一心两用
while(true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
实现Runnable接口创建线程
class MyRunnable implements Runnable{
@Override
public void run() {
while(true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Demo2 {
public static void main(String[] args) throws InterruptedException {
//好处是:高内聚,低耦合
Runnable runnable=new MyRunnable();
//创建 Thread 类实例,调⽤ Thread 的构造⽅法时将 Runnable 对象作为目标参数.
Thread t=new Thread(runnable);//给Thread传参数进去
t.start();
//主线程
while(true){
System.out.println("hello main");
Thread.sleep(1000);
}
}
}
匿名内部类创建线程
Java中方法必须基于类来存在,使用匿名内部类就可以少定义一些类了。
public class demo3 {
public static void main(String[] args) {
//匿名内部类
Thread t=new Thread(){
//属性
private int n;
//方法重写
@Override
public void run(){
while(true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
while(true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Demo4 {
public static void main(String[] args) throws InterruptedException {
//使用Runnable,任务和线程概念是分离的
Runnable runnable=new Runnable(){
@Override
public void run() {
while(true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread t=new Thread(runnable);//创建Thread实例
t.start();
while(true){
System.out.println("hello main");
Thread.sleep(1000);
}
}
}
public class Demo5 {
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) {
e.printStackTrace();
}
}
}) ;
t.start();
while(true){
System.out.println("hello main");
Thread.sleep(1000);
}
}
}
完结
如果这个系列博客对你有帮助的话,可以点一个免费的赞并收藏起来哟~
可以点点关注,避免找不到我~ ,我的主页:optimistic_chen
我们下期不见不散 ~ ~ ~