首页 文章

究竟是Java中的线程池在做什么?

提问于
浏览
3

线程池像任何ExecutorServices一样,我们定义了一个大小为3的newFixedPool . 现在我有一个大约10000个可运行任务的队列 . 为了执行上述过程,我有这些疑虑 -

  • 要执行上述过程,执行程序是否只允许任务中的3个线程一次运行?

  • 池将携带3个线程,这3个线程仅负责执行所有10000个任务 . 如果它是正确的,单个线程如何运行不同的可运行任务,因为最终这些任务也是线程本身,并且在运行任何作业/任务的过程中,您可以为池线程分配新的职责 .

2 回答

  • 5
    • 是的,如果事实上你正在使用 Executors.newFixedThreadPool(3) ,那么一次最多只有3个线程会在池中

    • 10,000个任务不是 Threads ,它们只是 Runnables . 必须通过 Thread#start 启动 Thread 才能实际创建系统线程 . 任务( Runnable 的实例)放在 BlockingQueue 中 . 线程池中的线程将轮询BlockingQueue以运行任务 . 当他们完成任务时,他们返回队列以获得另一个任务 . 如果添加了更多任务,则根据该队列的实现规则将它们插入到 BlockingQueue 中 . 对于大多数队列来说,这是先进先出,但 PriorityQueue 实际上使用 Comparator 或自然排序来在插入任务时对其进行排序 .

  • 0

    Below is customThreadPool in java which accept noofThreads and MaxConcurrentTask. Also it has stop() to stop complete ThreadPool

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
    
    
    @SuppressWarnings("all")
    public class ThreadPool { 
    
    
        private BlockingQueue taskQueue = null;  
        private List<PoolThread> threads = new ArrayList<PoolThread>(); 
        private boolean isStopped = false; 
    
        public ThreadPool(int noOfThreads, int maxNoOfTasks) { 
            taskQueue = new LinkedBlockingQueue(maxNoOfTasks);
            for(int i=0; i<noOfThreads; i++) {    
                threads.add(new PoolThread(taskQueue));  
            }   
    
            for(PoolThread thread : threads) {    
                thread.start();  
            }  
        }  
    
        public synchronized void execute(Runnable task) {  
    
            if(this.isStopped) 
                throw  new IllegalStateException("ThreadPool is stopped");
    
            this.taskQueue.offer(task); 
        }  
    
        public synchronized void stop() {  
            this.isStopped = true;  
            for(PoolThread thread : threads)    {     
                thread.stopMe();   
            } 
        }
    }
    
    
    @SuppressWarnings("all")
    class PoolThread extends Thread { 
    
        private BlockingQueue taskQueue = null; 
        private boolean       isStopped = false; 
        public PoolThread(BlockingQueue queue)  { 
            taskQueue = queue; 
        } 
    
        public void run()   {   
            while(!isStopped()) {   
                try {    
                    Runnable runnable = (Runnable) taskQueue.poll();  
                    runnable.run();     
                } catch(Exception e)    {    
                    //log or otherwise report exception,        //but keep pool thread alive.  
    
                }    
            } 
        } 
    
        public synchronized void stopMe()   {  
            isStopped = true;   
            this.interrupt(); 
            //break pool thread out of dequeue() call. 
        }  
    
        public synchronized boolean isStopped() {  
            return isStopped;  
        }
    }
    

相关问题