并发编程之线程基础

一、基础概念

CPU核心数和线程数的关系

核心数:线程数 = 1:1;

对于英特尔发布的超线程技术–>1:2

1.物理cpu数:主板上实际插入的cpu数量,可以数不重复的 physical id 有几个(physical id)2.cpu核数:单块CPU上面能处理数据的芯片组的数量,如双核、四核等 (cpu cores)

3.逻辑cpu数:一般情况下,逻辑cpu=物理CPU个数×每颗核数,如果不相等的话,则表示服务器的CPU支持超线程技术(HT:简单来说,它可使处理器中的1 颗内核如2 颗内核那样在操作系统中发挥作用。这样一来,操作系统可使用的执行资源扩大了一倍,大幅提高了系统的整体性能,此时逻辑cpu=物理CPU个数×每颗核数x2)

在Windows中,在cmd命令中输入“wmic”,然后在出现的新窗口中输入“cpu get *”即可查看物理CPU数、CPU核心数、线程数。其中,
Name:表示物理CPU数
NumberOfCores:表示CPU核心数
NumberOfLogicalProcessors:表示CPU线程数

在任务管理器中也可查看,笔者的计算机cpu信息如下:
在这里插入图片描述
由此可发现,笔者计算机物理CPU个数为1,每颗核数为4,并采取了超线程技术,所以逻辑处理器为8,

支持最大并行线程数为8。

System.out.println("当前电脑逻辑处理器数量为:");
     System.out.println(Runtime.getRuntime().availableProcessors());//availableProcessors()获取当前电脑逻辑处理器数量

CPU时间片轮转机制

又称RR调度,系统把所有就绪进程按先入先出的原则排成一个队列。新来的进程加到就绪队列末尾。每当执行进程调度时,进程调度程序总是选出就绪队列的队首进程,让它在 CPU 上运行一个时间片的时间。时间片是一个小的时间单位,通常为 10~100ms 数量级。当进程用完分给它的时间片后,系统的计时器发出时钟中断,调度程序便停止该进程的运行,把它放入就绪队列的末尾;然后,把 CPU 分给就绪队列的队首进程,同样也让它运行一个时间片,如此往复。

进程和线程

进程:程序运行资源分配的最小单位

线程:CPU调度的最小单位

并行与并发

并行:同一时刻可以处理事情的能力

并发:与时间单位相关的,在单位时间内可以处理事情的能力

高并发编程的意义、好处和注意事项

好处和意义:

  • 充分利用CPU资源
  • 减少响应用户的时间
  • 使代码模块化、异步化

注意事项(难点)

  • 共享资源 --> 存在冲突 --> 高并发情况下可能造成死锁
  • 太多线程任务可能加大计算机负担,造成计算机崩溃

二、线程基础

首先可以看一下基本程序中的线程情况:

public static void main(String[] args) {
        //查看当前线程情况
        System.out.println("当前线程情况为:");
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        ThreadInfo[] threadMXBeans = threadMXBean.dumpAllThreads(false, false);
        for (ThreadInfo mxBean : threadMXBeans) {
            System.out.println(mxBean.getThreadId() + "+++" + mxBean.getThreadName());
        }
    }

运行结果:

当前线程情况为:
6+++Monitor Ctrl-Break
5+++Attach Listener
4+++Signal Dispatcher
3+++Finalizer
2+++Reference Handler
1+++main

发现就算是一个简单的main函数,也会启动多个线程。

创建线程的三种方式:继承Thread、实现Runnable、实现Callable,前两种方式在之前的博客中已经做了详细介绍,此处介绍第三种。

通过查阅文档可知:

Interface Callable

  • 参数类型

    V - 方法的结果类型 call

    • Functional Interface:

    这是一个功能界面,因此可以用作lambda表达式或方法引用的赋值对象。

    • @FunctionalInterface
      public interface Callable<V>
      

      返回结果并可能引发异常的任务。实现者定义一个没有参数的单一方法,称为call

      Callable接口类似于Runnable,因为它们都是为其实例可能由另一个线程执行的类设计的。 然而,Runnable不返回结果,也不能抛出被检查的异常。

      该Executors类包含的实用方法,从其他普通形式转换为Callable类。

Class FutureTask

  • public class FutureTask<V>
    extends Object
    implements RunnableFuture<V>
    
    • FutureTask可用于包装CallableRunnable对象。 因为FutureTask实现Runnable ,一个FutureTask可以提交到一个Executor执行。

由此可知Callable使用方法:

//使用Callable实现线程
public class CallableTest implements Callable<String> {
    public static void main(String[] args) throws Exception {
        CallableTest callableTest = new CallableTest();
        FutureTask<String> futureTask = new FutureTask<>(callableTest);
        Thread c = new Thread(futureTask);
        c.start();
        System.out.println(futureTask.get());
    }

    @Override
    public String call() throws Exception {
        return "HelloWorld";
    }

}
public static void main(String[] args) throws Exception {
    Callable<String> callableTest = ()->"HelloWorld";
    FutureTask<String> futureTask = new FutureTask<>(callableTest);
    Thread c = new Thread(futureTask);
    c.start();
    System.out.println(callableTest.call());

}

执行结果:

HelloWorld

Process finished with exit code 0

线程的安全停止:
怎样才会让线程安全的停止工作呢?

1.自然执行完;

2.抛出异常;

3.stop()、resume()、suspend(),线程不会释放资源;

4.interrupt()、static方法的Interrupted()

  • interrupt()中断一个线程,并不是强制关闭这个线程,打个招呼,中断标志为true
  • isInterrupted() 判定当前线程是否处于中断状态

此处介绍第四种方法:

首先是继承Thread的的安全结束:

public class EndThread extends Thread {
    public static void main(String[] args) throws InterruptedException {
        Thread endThread = new EndThread();
        endThread.start();
        Thread.sleep(20);
        endThread.interrupt();
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        while(!isInterrupted()) System.out.println(name+" is running");
        System.out.println(name+" isInterrupted is  " +isInterrupted());
    }
}

运行结果截取:

Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 isInterrupted is  true

实现Runnable的安全结束:

public class EndRunnable {
    public static void main(String[] args) throws InterruptedException {
        Thread c = new Thread(()->{
            String name = Thread.currentThread().getName();
            boolean interrupted;
            while(!(interrupted = Thread.currentThread().isInterrupted())) System.out.println(name+" is running");
            System.out.println(name+" isInterrupted is  " +interrupted);
        });
        c.start();
        Thread.sleep(20);
        c.interrupt();
    }
}

运行结果截取:

Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 isInterrupted is  true

此处有一个地方需要注意:

在出现InterruptedException异常的情况下,线程终止会被打断,因此需要进行二次终止

//InterruptedException:线程在等待,睡眠或以其他方式占用时抛出,线程在活动之前或活动期间中断。 偶尔,一个方法可能希望测试当前线程是否已被中断,如果是,立即抛出该异常。
public class HasInterruptedException {
    public static void main(String[] args) throws InterruptedException {
        Thread c = new Thread(()->{
            String name = Thread.currentThread().getName();
            Thread thread = Thread.currentThread();
            while(!thread.isInterrupted()){
                try {
                    Thread.sleep(100);
                    System.out.println(name+" isInterrupted is "+ thread.isInterrupted());
                } catch (InterruptedException e) {
                    System.out.println(name+" isInterrupted is "+ thread.isInterrupted());
                    System.out.println(name+" 发生了异常");
                    //thread.interrupt();
                    //在出现InterruptedException异常的情况下,线程终止被打断,因此需要进行二次终止
                    e.printStackTrace();
                }
            }
        });
        c.start();
        Thread.sleep(500);
        c.interrupt();
    }
}

运行结果节选:

Thread-0 isInterrupted is false
Thread-0 isInterrupted is false
Thread-0 发生了异常
java.lang.InterruptedException: sleep interrupted
	at java.lang.Thread.sleep(Native Method)
	at com.yld.thread.HasInterruptedException.lambda$main$0(HasInterruptedException.java:11)
	at java.lang.Thread.run(Thread.java:748)
Thread-0 isInterrupted is false
Thread-0 isInterrupted is false
Thread-0 isInterrupted is false
Thread-0 isInterrupted is false
Thread-0 isInterrupted is false

在这个例子里面 线程c 休眠被打断 引发 InterruptedException 异常,此时isInterrupted为false,状态修改失败,从而使线程无法关闭。

这时候需要在cause里面进行二次中断,如代码注释。

二次中断后:

Thread-0 isInterrupted is false
Thread-0 isInterrupted is false
Thread-0 isInterrupted is false
Thread-0 isInterrupted is false
Thread-0 isInterrupted is false
Thread-0 发生了异常
java.lang.InterruptedException: sleep interrupted
	at java.lang.Thread.sleep(Native Method)
	at com.yld.thread.HasInterruptedException.lambda$main$0(HasInterruptedException.java:11)
	at java.lang.Thread.run(Thread.java:748)

Process finished with exit code 0

线程状态关系

在这里插入图片描述

守护线程:与主线程共死(主线程退出,守护线程一定退出),使用一个代码示例进行特性简单介绍

public class DaemonThread {
    public static void main(String[] args) throws InterruptedException {
        Thread c = new Thread(()->{
            try{
                String name = Thread.currentThread().getName();
                boolean interrupted;
                while(!(interrupted = Thread.currentThread().isInterrupted())) System.out.println(name+" is running");
                System.out.println(name+" isInterrupted is  " +interrupted);
            }finally {
                System.out.println("++++++++++++++");//守护线程的finally不一定会执行
            }
        });
        c.setDaemon(true);//守护线程设置
        c.start();
        Thread.sleep(20);
    }
}

运行结果节选:

Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Process finished with exit code 0

由上述例子可知,通过setDaemon方法将线程c设置成为main线程的守护线程,随着main线程的消亡,线程c也随之消亡。

注意:守护线程的finally不一定会执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值