线程初步认识

什么shi线程

线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际单位

一个进程有多个线程,通过cup的切换来

为什么要用多线程

如果我们同时操作两个文档,这时候我么用多线程 这样有提高我们性能,
异步执行
利用多CPU 资源实现真正意义上的并行执行

线程应用场景

多线程实现文件下载
后台任务,如向大量用户发送邮件
异步处理信息,记录日志
多步骤的任务处理,多任务分割,由一个主线程分割给多个线程完成,

总的来说
合理利用cpu资源来实现线程的并行处理,来实现一个线程内的多个任务的并行执行,同时基于线程本身的异步特性实现任务处理的效率

  • 继承 Thread 类
public class ThreadDome extends Thread {
    @Override
    public void run() {
        System.out.println("dangqi--:"+Thread.currentThread().getName());
    }
    public static void main(String[] args) {
        ThreadDome threadDome = new ThreadDome();
        threadDome.start();
    }
}
  • 实现Runable 接口
public class Runnable implements java.lang.Runnable {

    public void run() {
        System.out.println("dangqi"+Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable();
        Thread thread = new Thread(runnable);
        thread.start();

    }
}
  • 实现 Callable接口
public class Callable implements java.util.concurrent.Callable {
    public Object call() throws Exception {
        System.out.println("当前"+Thread.currentThread().getName());
        return "Callable";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        Future submit = executorService.submit(new Callable());
        System.out.println(submit.get());
    }
}

线程生命周期

  • new 初始状态 线程构建,但还没有调用Start方法,
  • runnabled 运行状态,jvm线程把操作系统中的就绪状态和运行状态统一称为行中”
  • blocked 阻塞状态 表示线程进入等待状态 因某种原因放弃cpu
  • waiting 等待状态
  • time_wating 超时等待状态,超时自动返回
  • terminated 终止状态,表示当前线程执行完毕
    在这里插入图片描述

Thread.sleep(0)的意义 就
是在o的时候让出cpu后0之后抢占cpu,可以说是重新分配cup的一个过程

Thread.sleep(0)工作流程

  • 挂起线程修改运行状态
  • 用sleep()提供的参数来设置一个定时器
  • 当时间结束,定时器触发,内核收到中断后修改线程的运行状态

wait() 和notifyAll();,notify()

  • wait()使当前线程进入等待队列,并释放该线程所拥有的锁,(而sleep()方法挂起当前线程,并不释放该线程所拥有的锁)等待某个条件发生变化,再由其他线程唤醒(notify)。
  • wait()方法可以有一个参数,就是等待的最长时间,若等待时长超过最长时间,该线程会从wait()中恢复执行。
  • notify()用来唤醒由wait()引起的被挂起的线程,当一个线程调用notify()时,在众多等待同一个锁的任务中的其中一个会被唤醒。
  • notifyAll()将唤醒所有等待当前线程所拥有的锁的线程,
wait() 和notify实现多个线程之间的通信如生产者和消费者

生产者

public class Producer implements Runnable {
    private Queue<String> bags;
    private int size;

    public Producer(Queue<String> bags, int size) {
        this.bags = bags;
        this.size = size;
    }


    @Override
    public void run() {
        try {
            int i = 0;
            while (true) {
                i++;
                synchronized (bags) {
                    while (bags.size() == size) {
                        System.out.println("bags满了");
                        //TODO 阻塞
                        bags.wait();
                    }
                    Thread.sleep(1000);
                    System.out.println("生产者生一个" + i);
                    bags.add("bag" + i);
                    //唤醒阻塞下的消费者
                    bags.notifyAll();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

消费者



public class Consumer implements Runnable {

    private Queue<String> bags;
    private int size;

    public Consumer(Queue<String> bags, int size) {
        this.bags = bags;
        this.size = size;
    }

    @Override
    public void run() {
        try {
            while (true) {
                synchronized (bags) {
                    while (bags.isEmpty()) {
                        System.out.println("bags为空了");
                        bags.wait();
                    }
                    Thread.sleep(1000);
                    String remove = bags.remove();
                    System.out.println("消费者消费" + remove);
                    //唤醒阻塞下的生产着
                    bags.notifyAll();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

所谓的生产着和消费者
他们是双向阻塞双向唤醒的一个操作
他们共用一个队列 共用一个size大小
就像生产商的产品,放到商店里,这时候消费者吧这个产品消费了,这是一个正常的关系,但是如果商店的产品放满了,商店告诉生产商先等一等在生产,但是如果商店的产品消费完了,商店告诉消费者等一等,这时候通过某种传递 ,消费者唤醒生产者,开始生产,,这时候有产品,生产商有传递给消费者有产品了可以消费了,就是唤醒消费者

线程中断

  • 线程处于阻塞下(join() wait())
  • while()循环
    线程结束: 是ran方法运行结束
public class stopDemo {
    private  static  int i;

    public static void main(String[] args) {
        Thread thread  = new Thread(()->{
            while (!Thread.currentThread().isInterrupted()) 
                i++;
        });
        thread.start();
        thread.interrupt(); //中断线程
    }
  thread.interrupt();结束线程 
Thread.currentThread().isInterrupted()

interrupted 返回一个中断标志
看实例

public class StopInterr {
    private  static  int i;
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() ->{
            while (true){
                if (Thread.currentThread().isInterrupted()){
                    System.out.println("be"+Thread.currentThread().isInterrupted());
                    Thread.interrupted();
                    System.out.println("uf"+Thread.currentThread().isInterrupted());
                }
            }
        });
        thread.start();
        thread.sleep(1000);
        thread.interrupt();
    }
}
打印======
betrue
uffalse

相当于开关复位一样。

线程停止

interrupt() 停止线程,
-> 主动停止run执行完,

在环境运行中:当死锁我们应该查看top命令看看,来查看线程的状态,

1.jsp
2. jstack

在这里插入图片描述

看着两个线程,能看出那个线程在竞争,还有所得状态

  • 当线程处于满负荷状态下,
    -当Cup处于满符合状态。看PID

top -c
在这里插入图片描述

查看进程id
top -H -p  7432定位到进程那个id最消耗cpu线程,

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
查出这个进程下的线程最站cup
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值