java线程-2

209 阅读3分钟

问题

1.多线程的目的是什么?

  • 充分利用cpu资源,并发作多件事。

2.如何在java程序中创建一个多线程?

  • new Thread()

3.线程执行的是什么?

  • 一条代码执行流,完成一组代码的执行,称他为一个任务。

4.任务是什么?

  • Runnable对象中的run()方法内的代码块。

5.线程是不是越多越好?

  • 时间资源:

    线程在java中是一个对象(所以每个线程 的创建销毁都需要时间,如果创建+销毁的时间>任务执行时间,要他有何用!),而且每个java线程都需要一个操作系统线程支持,大家都想要被运行,这时操作系统需要频繁切换线程,过多线程影响性能。

  • 空间资源:

    java对象占用堆内存,操作系统线程占用系统内存。根据jvm规范,一个线程默认最大栈大小为1M,这个站空间是需要从系统内存中分配的。

    6.线程的本质?

    • 将代码送给CPU执行。

    7.线程池:多个任务,用合适数量的线程不断运送代码即可,这合适数量的线程就构成了一个线程池,有任务要执行,就放入池中。池中的一个线程将任务送达cpu。


自己写一个线程池:

1.概念:

  • 核心线程:常驻,不会关的
  • 最大:超出核心线程数量创建的线程,可能会被线程池自己销毁。

2 .

3.仓库用什么?

BlockingQueue 是阻塞队列,线程安全

BlockingQueue操作形式有四种,处理方式不同:

  • 抛出异常:
  • 返回一个特殊值(null或false,具体取决于操作);
  • 在操作可以成功以前,无期限的祖册当前线程;
  • 在放弃前只在给定的最大时间限制内阻塞;
名称 抛出异常 特殊值 阻塞 超时
插入 add(e) offer(e) put(e) offer(time,e,unit)
移除 remove() poll() take() poll(time,unit)
检查 element() peek() 不可用 不可用

4.线程五种状态:

new——runnable——blocked——waiting——timed-waiting——terminated

5.代码


package thisl;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class MyThreadPool {

    private int corePoolSize;
    private int maxPoolSize;
    LinkedBlockingQueue<Runnable> workQueue = null;  //任务仓库


    public MyThreadPool(int corePoolSize, int maxPoolSize, int workQueueSize) {
        this.corePoolSize = corePoolSize;
        this.maxPoolSize = maxPoolSize;
        workQueue = new LinkedBlockingQueue<>(workQueueSize);
    }

    //1.线程池线程干什么?
    public class Worker extends Thread {
        Runnable firstTask;
        public Worker(Runnable firstTask){
            this.firstTask=firstTask;
        }
        @Override
        public void run() {
            try {
                //执行用户提交的runnable
                Runnable task = null;
                while (task != null || (task = workQueue.take() != null)) {
                    task.run();
                    task = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("线程池中的一个线程出现异常");
            }

        }
    }


    //J.U.C——并发编程工具包——基础类型原子操作封装
    AtomicInteger currentPoolSize=new AtomicInteger(0);  //当前线程池大小

    //2.execute提交任务
    public void execute(Runnable task) {
        //1.创建线程的步骤
        if (currentPoolSize.get() < corePoolSize) {   //做一次数量判断
            if(currentPoolSize.incrementAndGet()<=corePoolSize){    //incrementAndGet即实现加一
                new Worker(task).start();  //复用多个
                return;
            }
            else{
                currentPoolSize.decrementAndGet();

            }//多个线程同时调用,出现问题)

        }
        //2.提交到任务仓库
        if(workQueue.offer(task)){
            return;  //代表提交成功
        }

        //3. 是否达到线程池最大数量?没达到,则创建一个新的工作线程来执行任务。
        if(currentPoolSize.get()<maxPoolSize){
            if(currentPoolSize.incrementAndGet()<=maxPoolSize)
            {
                new Worker(task).start();  //线程状态切换
                return ;
            }
            else{
                currentPoolSize.decrementAndGet();
            }

        }

        //4.拒绝处理这个任务
        throw  new Exception("拒绝执行");
    }
}