• java ThreadPool


    package com.karl.threadpool;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class MyThreadPool {
    
        List<MyTask> tasks = Collections.synchronizedList(new ArrayList<MyTask>());
    
        private static MyThreadPool instance = MyThreadPool.getInstance();
    
        public static MyThreadPool getInstance() {
            if (instance == null) {
                instance = new MyThreadPool();
    
            }
            return instance;
        }
    
        private WorkThread[] threads;
    
        private MyThreadPool() {
            threads = new WorkThread[10];
            for (int i = 0; i < 10; i++) {
                threads[i] = new WorkThread();
            }
        }
    
        public void addTask(MyTask task) {
            synchronized (tasks) {
                tasks.add(task);
                tasks.notifyAll();
            }
        }
    
        class WorkThread extends Thread {
    
            public WorkThread() {
                start();
            }
    
            @Override
            public void run() {
                while (true) {
                    synchronized (tasks) {
                        if (tasks.isEmpty()) {
                            try {
                                // When call the method wait or notify of a Object,
                                // must ensure the Object is synchronized.
                                // Here let the Thread which use the object wait a
                                // while.
                                tasks.wait(20);
                                continue;
                            } catch (InterruptedException e) {
                            }
                        }
                        MyTask r = tasks.remove(0);
                        if (r != null) {
                            if (r.needRunImmde()) {
                                new Thread(r).start();
                            } else {
                                r.run();
                            }
                        }
                    }
                }
            }
        }
    
    }
    package com.karl.threadpool;
    
    public abstract class MyTask implements Runnable{
        protected abstract boolean needRunImmde();
        public void run(){
            
        }
    }
    package com.karl.threadpool;
    
    public class PrintNumber extends MyTask {
        
        private int index;
        private boolean needRunImmde=false;
        
        public boolean isNeedRunImmde() {
            return needRunImmde;
        }
    
        public void setNeedRunImmde(boolean needRunImmde) {
            this.needRunImmde = needRunImmde;
        }
    
        public PrintNumber(int index){
            this.index=index;
        }
    
        @Override
        protected boolean needRunImmde() {
            // TODO Auto-generated method stub
            return needRunImmde;
        }
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            System.out.println(index);
        }
    
    }
    package com.karl.threadpool;
    
    public class Test {
        public static void main(String[] args) throws InterruptedException {
            
            for (int i = 0; i < 10; i++) {
                PrintNumber pn = new PrintNumber(i);
                if((i%2)==0){
                    pn.setNeedRunImmde(false);
                }
                
                MyThreadPool.getInstance().addTask(pn);
            }
            Thread.sleep(10000L);
            for (int i = 0; i < 10; i++) {
                PrintNumber pn = new PrintNumber(i);
                if((i%2)==0){
                    pn.setNeedRunImmde(false);
                }
                
                MyThreadPool.getInstance().addTask(pn);
            }
        }
    }
  • 相关阅读:
    日期正则表达式yyyyMMdd
    Markdown语法
    su: Authentication failure问题
    Git初始配置
    layui的layer.open()方法查看缩略图 原图缩放
    入驻博客园三年
    php学习笔记之动态生成一组单选button
    opencv直方图拉伸
    c++ 中const的使用
    LeetCode131:Palindrome Partitioning
  • 原文地址:https://www.cnblogs.com/zhonghan/p/2889789.html
Copyright © 2020-2023  润新知