广州南站在哪个区如何搭建网络论坛平台

pingmian/2025/10/13 14:18:29/文章来源:
广州南站在哪个区,如何搭建网络论坛平台,创意网,电信固定ip如何做网站自定义java线程池ThreadPoolExecutor是Java并发api添加的一项功能#xff0c;可以有效地维护和重用线程#xff0c;因此我们的程序不必担心创建和销毁线程#xff0c;也不必关注核心功能。 我创建了一个自定义线程池执行程序#xff0c;以更好地了解线程池执行程序的工作方… 自定义java线程池 ThreadPoolExecutor是Java并发api添加的一项功能可以有效地维护和重用线程因此我们的程序不必担心创建和销毁线程也不必关注核心功能。 我创建了一个自定义线程池执行程序以更好地了解线程池执行程序的工作方式。 功能性 它维护一个固定的线程池即使没有任务提交也创建线程并启动线程而ThreadPoolExecutor根据需要创建线程即每当将可运行对象提交给池且线程数小于核心池大小时。 在ThreadPoolExecutor中我们提供了一个等待队列当所有线程忙于运行现有任务时新的可运行任务将在该队列中等待。 队列填满后将创建最大线程池大小的新线程。 在MyThreadPool中我将可运行对象存储在链接列表中因此每个任务都将在列表中等待并且不受限制因此在此不使用maxPoolSize。 在ThreadPoolExecutor中我们使用Future Objects从任务中获取结果如果结果不可用则future.get方法将阻塞或者使用CompletionService。 在MyThreadPoolExecutor中我创建了一个名为ResultListener的简单接口用户必须提供对此的实现如他希望如何处理输出。 每个任务完成后ResultListener将获得带有任务输出的回调或者在发生任何异常的情况下将调用error方法。 调用shutdown方法时MyThreadPoolExecutor将停止接受新任务并完成剩余任务。 与ThreadPoolExecutor相比我提供了非常基本的功能我使用了简单的线程机制如waitnotifynotifyAll和join。 在性能方面它类似于ThreadPoolExecutor在某些情况下好一些。 如果您发现任何有趣的结果或改进方法请告诉我。 package com.util;import java.util.concurrent.Callable;/*** Run submitted task of {link MyThreadPool} After running the task , It calls* on {link ResultListener}object with {link Output}which contains returned* result of {link Callable}task. Waits if the pool is empty.* * author abhishek* * param */import java.util.concurrent.Callable; /** * Run submitted task of {link MyThreadPool} After running the task , It calls * on {link ResultListener}object with {link Output}which contains returned * result of {link Callable}task. Waits if the pool is empty. * * author abhishek * * param V */ public class MyThreadV extends Thread {/*** MyThreadPool object, from which the task to be run*/private MyThreadPoolV pool;private boolean active true;public boolean isActive() {return active;}public void setPool(MyThreadPoolV p) {pool p;}/*** Checks if there are any unfinished tasks left. if there are , then runs* the task and call back with output on resultListner Waits if there are no* tasks available to run If shutDown is called on MyThreadPool, all waiting* threads will exit and all running threads will exit after finishing the* task*/public void run() {ResultListenerV result pool.getResultListener();CallableV task;while (true){task pool.removeFromQueue();if (task ! null){try{V output task.call();result.finish(output);} catch (Exception e){result.error(e);}} else{if (!isActive())break;else{synchronized (pool.getWaitLock()){try{pool.getWaitLock().wait();} catch (InterruptedException e){// TODO Auto-generated catch blocke.printStackTrace();}}}}}}void shutdown() {active false;} }package com.util; import java.util.LinkedList; import java.util.concurrent.Callable; /** * This class is used to execute submitted {link Callable} tasks. this class * creates and manages fixed number of threads User will provide a * {link ResultListener}object in order to get the Result of submitted task * * author abhishek * * */ public class MyThreadPoolV {private Object waitLock new Object();public Object getWaitLock() {return waitLock;}/*** list of threads for completing submitted tasks*/private final LinkedListMyThreadV threads;/*** submitted task will be kept in this list untill they run by one of* threads in pool*/private final LinkedListCallableV tasks;/*** shutDown flag to shut Down service*/private volatile boolean shutDown;/*** ResultListener to get back the result of submitted tasks*/private ResultListenerV resultListener;/*** initializes the threadPool by starting the threads threads will wait till* tasks are not submitted** param size* Number of threads to be created and maintained in pool* param myResultListener* ResultListener to get back result*/public MyThreadPool(int size, ResultListenerV myResultListener) {tasks new LinkedListCallableV();threads new LinkedListMyThreadV();shutDown false;resultListener myResultListener;for (int i 0; i size; i) {MyThreadV myThread new MyThreadV();myThread.setPool(this);threads.add(myThread);myThread.start();}}public ResultListenerV getResultListener() {return resultListener;}public void setResultListener(ResultListenerV resultListener) {this.resultListener resultListener;}public boolean isShutDown() {return shutDown;}public int getThreadPoolSize() {return threads.size();}public synchronized CallableV removeFromQueue() {return tasks.poll();}public synchronized void addToTasks(CallableV callable) {tasks.add(callable);}/*** submits the task to threadPool. will not accept any new task if shutDown* is called Adds the task to the list and notify any waiting threads** param callable*/public void submit(CallableV callable) {if (!shutDown) {addToTasks(callable);synchronized (this.waitLock) {waitLock.notify();}} else {System.out.println(task is rejected.. Pool shutDown executed);}}/*** Initiates a shutdown in which previously submitted tasks are executed,* but no new tasks will be accepted. Waits if there are unfinished tasks* remaining**/public void stop() {for (MyThreadV mythread : threads) {mythread.shutdown();}synchronized (this.waitLock) {waitLock.notifyAll();}for (MyThreadV mythread : threads) {try {mythread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} }package com.util;/*** This interface imposes finish method * which is used to get the {link Output} object * of finished task* author abhishek** param */public interface ResultListener {public void finish(T obj);public void error(Exception ex);} 您可以根据需要实现此类并返回并处理任务返回的结果。 package com.util;public class DefaultResultListener implements ResultListener{Overridepublic void finish(Object obj) {}Overridepublic void error(Exception ex) {ex.printStackTrace();}} 例如此类将添加task返回的数字。 package com.util;import java.util.concurrent.atomic.AtomicInteger;/*** ResultListener class to keep track of total matched count* author abhishek* * param */ public class MatchedCountResultListenerimplements ResultListener{/*** matchedCount to keep track of the number of matches returned by submitted* task*/AtomicInteger matchedCount new AtomicInteger();/*** this method is called by ThreadPool to give back the result of callable* task. if the task completed successfully then increment the matchedCount by* result count*/Overridepublic void finish(V obj) {//System.out.println(count is obj);matchedCount.addAndGet((Integer)obj);}/*** print exception thrown in running the task*/Overridepublic void error(Exception ex) {ex.printStackTrace();}/*** returns the final matched count of all the finished tasks* * return*/public int getFinalCount() {return matchedCount.get();} } 这是一个测试类使用CompletionService和MyThreadPoolExecutor对循环运行简单 package test;import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future;import com.util.DefaultResultListener; import com.util.MyThreadPool;public class TestClass {public static void main(String[] args) throws InterruptedException {CompletionServicethreadService;ExecutorService service Executors.newFixedThreadPool(2);threadService new ExecutorCompletionService(service);long b System.currentTimeMillis();for(int i 0;i50000;i){threadService.submit(new MyRunable (i));}service.shutdown();System.out.println(time taken by Completion Service (System.currentTimeMillis()-b));DefaultResultListener result new DefaultResultListener();MyThreadPoolnewPool new MyThreadPool(2,result);long a System.currentTimeMillis();int cc 0;for(int i 0;i50000;i){cc cci;}System.out.println(time taken without any pool (System.currentTimeMillis()-a));a System.currentTimeMillis();for(int i 0;i5000;i){newPool.submit(new MyRunable (i));}newPool.stop();System.out.println(time taken by myThreadPool (System.currentTimeMillis()-a));}}class MyRunable implements Callable{int index -1;public MyRunable(int index){this.index index;}Overridepublic Integer call() throws Exception {return index;}} 参考 我的JCG合作伙伴 Abhishek Somani在JavaJ2EE和Server博客上的Java 自定义线程池执行程序 。 翻译自: https://www.javacodegeeks.com/2013/03/my-custom-thread-pool-executor-in-java.html自定义java线程池

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/88210.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一站式网站建设方案企业邮箱网易登录入口

Linux 虚拟机中网络连接的三种方式 先假设一个场景,在教室中有三个人:张三、李四和王五(这三个人每人有一台主机),他们三个同处于一个网段中(192.169.0.XX),也就是说他们三个之间可…

佛山企业网站建设机构遵义网信办

WPF 如何让UI的xmal 按照下面的格式化显示 首先格式化显示在VS中的快捷键是 Ctrl KD 然后需要配置,工具 选项 -文本编辑器 -xmal -格式化-间距 更改成如下就可以了

二手网站设计与建设最新外贸seo

文章目录 5.1 树的基本概念5.1.1 树的定义5.1.2 森林的定义5.1.3 树的术语5.1.4 树的表示 5.2 二叉树5.2.1 二叉树1. 定义2. 特点3. 性质引理5.1:二叉树中层数为i的结点至多有 2 i 2^i 2i个,其中 i ≥ 0 i \geq 0 i≥0。引理5.2:高度为k的二叉…

网站建设价格费用石家庄鹿泉网站建设

CSRF(Cross-Site Request Forgery,跨站请求伪造)是一种常见的网络安全攻击方式,攻击者利用用户已经通过认证的身份在受信任网站上执行未经用户授权的操作。 CSRF 攻击的一般过程如下: 用户登录受信任网站 A&#xff…

沙漠风网站开发怎样打造一个app需要多少钱

sass的优缺点 优点:css预处理器为css增加一些编程的特性,无需考虑浏览器的兼容性问题。支持嵌套、变量和逻辑等。可以让css更加简介、提高代码复用性、逻辑分明等等。 缺点:css的文件体积和复杂度不可控;增加了调试难度和成本 常用…

互联网营销师题库及答案同时做几个网站的seo

UltraCompare是一款功能强大的文件和文件夹比较工具,用于比较和合并文本、二进制和文件夹。它提供了丰富的功能和直观的界面,使用户能够轻松地比较和同步文件内容,查找差异并进行合并操作。 以下是UltraCompare软件的一些主要特点和功能&…

网站结构分析具体分析内容包头网站建设 奥北

#usage: go build [-o output] [-i] [build flags] [packages] go build的使用比较简洁,所有的参数都可以忽略,直到只有go build,这个时候意味着使用当前目录进行编译,下面的几条命令是等价的: go buildgo build .go b…

网站设计文档谁用腾讯风铃做网站的

环境 如上图所示, Runtime version的版本是JAVA 17 项目所需要JDK版本为JAVA 8 解决

响应式 网站建设免费广告行业网站建设

Atitit. 提升软件开发效率and 开发质量---java 实现dsl 4gl 的本质and 精髓 O725 1. DSL主要分为三类:外部DSL、内部DSL,以及语言工作台。 1 2. DSL规则 2 2.1. DSL 整洁的代码 2 2.2. DSL必须以文本代码的形式出现 2 2.3. DSL的语法应该尽可能地接近…

八方资源网做网站优化怎么样网页设计与制作教程第5版答案

标题:基于STM32F103C8T6单片机的1秒定时器设计与应用 摘要: 本文主要探讨了如何在STM32F103C8T6微控制器上利用内部定时器实现精确的1秒钟定时功能,并通过实际项目实施,验证其稳定性和可靠性。首先介绍了STM32F103C8T6单片机的特…

温州建设信息网站招聘求职网站html模板

一、VMware Workstation 虚拟机 先得安装 VM 虚拟机,没有的可以参考这篇文章安装 VM 虚拟机 如何在 VM 虚拟机中安装 Win10 操作系统保姆级教程(附链接)https://eclecticism.blog.csdn.net/article/details/135713915 二、Deft 镜像 下载…

网络app开发网站建设价格枣庄网站建设多少钱

一、原理图1. RS485接口6KV防雷电路设计方案图1 RS485接口防雷电路接口电路设计概述:RS485用于设备与计算机或其它设备之间通讯,在产品应用中其走线多与电源、功率信号等混合在一起,存在EMC隐患。本方案从…

长春网站建设大概需要多少钱建站技术服务

转载自 吃透这套架构演化图,从零搭建Web网站也不难 前言工作也有几多年了,无论是身边遇到的还是耳间闻到的,多多少少也积攒了自己的一些经验和思考,当然,博主并没有太多接触高大上的分布式架构实践,相对比较…

河北省水利建设市场网站网站外包合作

Oracle 发布对 Visual Studio Code 的 Java 插件支持,这个扩展插件通过基于 OpenJDK 的 javac 编译器和调试器接口的语言服务器,为流行的多语言集成开发环境提供 Java 支持。 VS Code 扩展的核心是Java语言服务器:这是一个使用语言服务器协议…

免费网站优化怎么做康定网站建设公司

AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id2844 这题貌似HDU上有一道差不多的题,不过我没做过,也就没管了。 首先讲一个线性基的东西,大概就是这样: 然后就是一个什么性质:S异或起来会出现重…

做网站有必要用wordpress网站建设运营推广

转载公众号 | 美团技术团队常识性概念图谱,是围绕常识性概念建立的实体以及实体之间的关系,同时侧重美团的场景构建的一类知识图谱。本文介绍了美团常识性概念图谱构建的Schema,图谱建设中遇到的挑战以及建设过程中的算法实践,最后…

济南网站建设哪家便宜有一个做ppt的网站吗

原题链接: 198. 打家劫舍 题目描述: 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入&a…

定制制作网站价格表wordpress sql查询分类

1 type_traits 的概述 type_traits 是 C 标准模板库(STL)中的一个头文件,它定义了一系列模板类,这些模板类在编译期获取某一参数、某一变量、某一个类等的类型信息,主要用于进行静态检查。通过使用 type_traits&#…

成都php网站制作程序员空壳网站清理

文章目录 roles批量替换文件 role 的依赖关系role 的实际案例 roles tasks 和 handlers ,那怎样组织 playbook 才是最好的方式呢?简 单的回答就是:使用 Roles Roles 基于一个已知的文件结构,去自动的加载 vars,tasks 以…

电脑制作网站用哪个软件深圳网页搜索排名提升

从事分布式服务器开发工作的都会遇到,linux下open_file的值默认是1024;max user processes的值默认是4096,在实际用于中,这两个值严重不足,常常需要调整这两个值。默认配置如下: 可以通过以下两种方式修改&…