线程基础(1)

本文介绍了线程的基本概念,阐述了线程的重要性,特别是在并发编程中的优势。详细讲解了Java中通过继承Thread类、实现Runnable接口以及匿名内部类创建线程的方法,以及线程与进程的区别。最后提到了线程池和协程等高级主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


目前在学习线程,希望看到的小伙伴可以支持一下.

线程概念

线程是什么

一个线程就是一个执行流,每个线程之间都可以按照顺序执行自己的代码,多个线程之间"同时"执行着多份代码.

为什么要有线程

如今,"并发编程"成为了刚需,并发编程能更充分的利用多核cpu资源.
虽然进程也可以实现并发编程,但是线程比进程更加轻量.

 - 创建线程比创建进程更快.
 - 销毁线程比销毁线程更快.
 - 调度线程比调度线程更快.
了解: 虽然线程比进程更加轻量,又出现了"线程池""协程"(Go语言中).

线程和进程的区别

- 进程是包含线程的,每个进程至少有一个线程存在,"主线程".
- 进程和线程不共享内存空间,同一个进程的线程之间共享同一个内存空间.
- 进程是系统分配资源的最小单位,线程是系统调度的最小单位.

java的线程和操作系统线程的关系

操作系统内核实现了线程这样的机制,并且对用户层提供了一些API供用户使用.
java标准库中Thread类视为对操作系统的API进行了一层封装和处理.

创建线程

通过继承Thread类

class MyThread extends Thread {
    // 线程入口
    @Override
    public void run() {
        //super.run();
        while(true) {
            System.out.println("thread");
            try {
            	// 1000ms后被唤醒
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        //start()方法会自动调用系统API,在系统中把线程对应的pcb创建出来并管理好
        // 新线程就参与调度了
        myThread.start();

        while(true) {
            System.out.println("main");
            // sleep(1000)之后,线程就会进入到阻塞状态
            // 时间到了,就会随机唤醒线程中的一个
            Thread.sleep(1000);
        }
    }
}

在这里插入图片描述
可以看出,也不是main线程一定在thread线程前面,也就是说,当这两个线程被唤醒之后,谁先调度,谁后调度,可以视为随机的.这种"随机"调度的方式,成为"抢占式执行".

官方在jdk中提供了一个调试工具,“jconsole”
在这里插入图片描述
直接连接!

在这里插入图片描述

在这里列出了当前线程的所有线程,不仅仅是主线程和自己创建的线程,剩下的线程都是JVM中自带的.
其中Thread-0就是我们自己创建的,它是这样命名的:Thead-1,Thread-2...

在这里插入图片描述

这是线程的调用栈,有方法之间的调用关系.
当程序卡死的时候,查看每个线程的调用栈,就能知道哪个代码出现卡死的情况.

实现Runnable接口

class MyRunnable implements Runnable {
    @Override
    public void run() {
        while(true) {
            System.out.println("thread");
            try {
                // 1000ms后被唤醒
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        // start()方法会自动调用系统API,在系统中把对应的pcb创建出来,并管理好
        // 线程可以参与调度了
        thread.start();
        while (true) {
            System.out.println("main");
            Thread.sleep(1000);
        }
    }
}

基于匿名内部类,继承Thread类

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread() {
            @Override
            public void run() {
                while(true) {
                    System.out.println("thread");
                    try {
                        // 1000ms后被唤醒
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        // 通过调用系统API,把对应的pcb创建出来,并管理好
        // 线程可以参与调度了
        thread.start();

        while (true) {
            System.out.println("main");
            Thread.sleep(1000);
        }
    }
}

基于匿名内部类,实现Runnable接口

public class Demo4 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    System.out.println("thread");
                    try {
                        // 1000ms后被唤醒
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        // 自动调用系统的API,创建对应的pcb,线程可以参与调度了
        thread.start();

        while(true) {
            System.out.println("main");
            Thread.sleep(1000);
        }
    }
}

lambda方式

public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while(true) {
                System.out.println("thread");
                try {
                    // 1000ms后被唤醒
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        // 自动调用系统的API,创建对应的pcb,线程可以参与调度了
        thread.start();

        while(true) {
            System.out.println("main");
            Thread.sleep(1000);
        }

    }
}
还有基于"线程池""Callable"的方法,放到后面补上.
Thread thread = new Thread(()->{
            while(true) {
                System.out.println("thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "myThread"); // 线程重命名

此时会对线程进行重命名,在jconsole工具中,我们自己创建的线程名字就被改为"myThread"了

结语

我写出了线程的部分基础,后续还会更新更多线程知识和干货,请大家多多支持.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值