并发编程学习之初体验

并发编程学习之初体验

并发编程学习之初体验

真正创建线程的方法是 new Thread()-->即途中的卡车

其余的实现Runnable接口,或者Callable接口然后交给Runnable等,其实都是任务代码,最后都是交给Thread去执行

并发编程学习之初体验

并发编程学习之初体验

首先:创建线程和销毁线程是需要消耗资源的,其次:创建的线程需要开辟内存空间进行存放

代码演示:

public class MixThread {
    public static void main(String[] args) {
        CountDownLatch cdl = new CountDownLatch(1);
        try {
            Thread.sleep(2000L);
        } catch (Exception e){
            e.printStackTrace();
        }
        for (int i = 0; i < 5000; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        cdl.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            System.out.println("i= " + i);
        }
    }
}

通过jdk自带的jvisualvm工具,直接打开可以查看到

并发编程学习之初体验

并发编程学习之初体验

并发编程学习之初体验

通过上面的图可以知道,堆内存最大值设置为100M,线程数5000

并发编程学习之初体验

并发编程学习之初体验

并发编程学习之初体验


手写一个简单的线程池

/**
* @author fmm
* @date 2020/10/11
* 手写一个固定大小的线程池
*  1. client-->提交任务到线程池的仓库
*  2. 从仓库提取任务到空闲线程执行,提取的方法有四种,返回不同的情况
*  3. 处理阻塞和争抢问题
*/
public class FixedSizeThreadPool {
    //仓库
    BlockingQueue queue;
    //工作线程
    List workers;
    //设置状态,当所有任务都执行完成后,isWorking=false
    volatile boolean isWorking = true;


    public FixedSizeThreadPool(int poolSize, int queueSize) {
        queue = new LinkedBlockingQueue<>(queueSize);
        workers = new ArrayList<>(poolSize);
        for (int i = 0; i < poolSize; i++) {
            Worker worker = new Worker(this);
            workers.add(worker);
            worker.start();
        }
    }


    //提交任务到仓库
    public boolean submit(Runnable task){
        return queue.offer(task);
    }


    public static void main(String[] args) {
        FixedSizeThreadPool pool = new FixedSizeThreadPool(10,3);
        for (int i = 0; i < 10; i++) {
            final int index = i;
            pool.submit(()->{
                System.out.println(Thread.currentThread().getName() + "执行任务:" + index);
            });
            try {
                Thread.sleep(200L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //不能接收新任务,当前的任务执行完后,线程池关闭(线程死亡)
        pool.shutdown();
    }


    private void shutdown() {
        //线程执行完,更改标志
        isWorking = false;
        //将阻塞的线程手动关闭
        for(Thread t : workers){
            if(t.getState()== Thread.State.BLOCKED || t.getState()== Thread.State.TIMED_WAITING
                || t.getState()== Thread.State.WAITING){
                t.interrupt();
            }
        }
    }


    static class Worker extends Thread{
        FixedSizeThreadPool threadPool;
        public Worker(FixedSizeThreadPool threadPool) {
            this.threadPool = threadPool;
        }


        @Override
        public void run(){
            while (threadPool.isWorking || threadPool.queue.size()>0){
                Runnable task = null;
                if(threadPool.isWorking) {
                    try {
                        //从仓库拿任务,take()方法当没有任务时会阻塞
                        task = threadPool.queue.take();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    task = threadPool.queue.poll();
                }
                if(task != null){
                    task.run();
                }
            }
        }
    }
}


并发编程学习之初体验

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章