当前位置: 首页 > news >正文

做铁艺需要什么网站/网站怎么进入

做铁艺需要什么网站,网站怎么进入,广告网站布局,wordpress增加主题配置以一个案例功能来初步认识一下java多线程编程。 Java多线程编程安全关键字volatile、synchronized,及api Executors|ExecutorService的基本用法 一.首先基础准备 volatile修饰符的作用:被volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中…

以一个案例功能来初步认识一下java多线程编程。

 

Java多线程编程安全关键字volatile、synchronized,及api Executors|ExecutorService的基本用法

 

一.首先基础准备

volatile修饰符的作用:被volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值。而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存。这样在任何时刻,多个不同的线程总是看到某个成员变量的同一个值。

synchronized修饰符的作用:被synchronized 关键字锁定的对象(方法、代码块、成员变量/对象)同一时间只能被一个线程访问,这样确保了在多线程环境的线程安全。

java 多线程相关api:Executors、ExecutorService、Thread、Runnable等相关api,此处不一一列出,可自行查阅Java™ Platform Standard Ed. 8文档。

1).Executors:java.util.concurrent.Executors

Class Executors  
All Methods  
Modifier and TypeMethod and Description 
static Callable<Object>callable(PrivilegedAction<?> action) 
Returns a Callable object that, when called, runs the given privileged action and returns its result.返回一个可调用对象,该对象在被调用时运行给定的特权操作并返回其结果。
static Callable<Object>callable
(PrivilegedExceptionAction<?> action)
 
Returns a Callable object that, when called, runs the given privileged exception action and returns its result.返回一个可调用对象,该对象在被调用时运行给定的特权异常操作并返回其结果。
static Callable<Object>callable(Runnable task) 
Returns a Callable object that, when called, runs the given task and returns null.返回一个可调用对象,该对象在被调用时运行给定的任务并返回null
static <T> Callable<T>callable(Runnable task, T result) 
Returns a Callable object that, when called, runs the given task and returns the given result.返回一个可调用的对象,该对象在被调用时运行给定的任务并返回给定的结果。
static ThreadFactorydefaultThreadFactory() 
Returns a default thread factory used to create new threads.返回用于创建新线程的默认线程工厂。
static ExecutorServicenewCachedThreadPool() 
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.创建一个线程池,该线程池根据需要创建新线程,但在以前构造的线程可用时将重用它们。
static ExecutorServicenewCachedThreadPool
(ThreadFactory threadFactory)
 
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available, and uses the provided ThreadFactory to create new threads when needed.创建一个线程池,该线程池根据需要创建新线程,但在以前构造的线程可用时将重用这些线程,并在需要时使用提供的ThreadFactory创建新线程。
static ExecutorServicenewFixedThreadPool
(int nThreads)
 
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.创建一个线程池,该线程池重用在共享无边界队列上运行的固定数量的线程。
static ExecutorServicenewFixedThreadPool
(int nThreads, ThreadFactory threadFactory)
 
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed.创建一个线程池,该线程池重用在共享无边界队列中运行的固定数量的线程,在需要时使用提供的ThreadFactory创建新线程。
static 
ScheduledExecutorService
newScheduledThreadPool(int corePoolSize) 
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.创建一个线程池,该线程池可以安排命令在给定延迟后运行,或定期执行。
static 
ScheduledExecutorService
newScheduledThreadPool
(int corePoolSize, ThreadFactory threadFactory)
 
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.创建一个线程池,该线程池可以安排命令在给定延迟后运行,或定期执行。
static ExecutorServicenewSingleThreadExecutor() 
Creates an Executor that uses a single worker thread operating off an unbounded queue.创建一个执行器,该执行器使用一个工作线程在无边界队列中进行操作。
static ExecutorServicenewSingleThreadExecutor
(ThreadFactory threadFactory)
 
Creates an Executor that uses a single worker thread operating off an unbounded queue, and uses the provided ThreadFactory to create a new thread when needed.创建一个执行器,该执行器使用单个工作线程在无界队列外运行,并在需要时使用提供的ThreadFactory创建新线程。
static 
ScheduledExecutorService
newSingleThreadScheduledExecutor() 
Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically.创建一个单线程执行器,该执行器可以安排命令在给定延迟后运行,或定期执行。
static 
ScheduledExecutorService
newSingleThreadScheduledExecutor
(ThreadFactory threadFactory)
 
Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically.创建一个单线程执行器,该执行器可以安排命令在给定延迟后运行,或定期执行。
static ExecutorServicenewWorkStealingPool() 
Creates a work-stealing thread pool using all available processors as its target parallelism level.使用所有可用的处理器作为其目标并行级别来创建工作线程池。
static ExecutorServicenewWorkStealingPool
(int parallelism)
 
Creates a thread pool that maintains enough threads to support the given parallelism level, and may use multiple queues to reduce contention.创建一个线程池,该线程池维护足够的线程以支持给定的并行级别,并且可以使用多个队列来减少争用。
static <T> Callable<T>privilegedCallable
(Callable<T> callable)
 
Returns a Callable object that will, when called, execute the given callable under the current access control context.返回一个可调用对象,该对象在被调用时将在当前访问控制上下文下执行给定的可调用对象。
static <T> Callable<T>privilegedCallableUsingCurrentClassLoader
(Callable<T> callable)
 
Returns a Callable object that will, when called, execute the given callable under the current access control context, with the current context class loader as the context class loader.返回一个可调用对象,该对象在被调用时,将在当前访问控制上下文下执行给定的可调用对象,并将当前上下文类装入器作为上下文类装入器。
static ThreadFactoryprivilegedThreadFactory() 
Returns a thread factory used to create new threads that have the same permissions as the current thread.返回用于创建与当前线程具有相同权限的新线程的线程工厂。
static ExecutorServiceunconfigurableExecutorService
(ExecutorService executor)
 
Returns an object that delegates all defined ExecutorService methods to the given executor, but not any other methods that might otherwise be accessible using casts.返回一个对象,该对象将所有已定义的ExecutorService方法委托给给定的executor,但不委托任何其他可以使用强制转换访问的方法。
static 
ScheduledExecutorService
unconfigurableScheduledExecutorService
(ScheduledExecutorService executor)
 
Returns an object that delegates all defined ScheduledExecutorService methods to the given executor, but not any other methods that might otherwise be accessible using casts.返回一个对象,该对象将所有已定义的ScheduledExecutorService方法委托给给定的执行器,但不委托任何其他可能使用强制转换访问的方法。

2).ExecutorService:java.util.concurrent.ExecutorService

Interface ExecutorService  
All Methods  
Modifier and TypeMethod and Description 
booleanawaitTermination(long timeout, TimeUnit unit) 
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.在关闭请求、超时或当前线程中断(以先发生的为准)之后,阻塞直到所有任务都已完成执行。
<T> List<Future<T>>invokeAll(Collection<? extends Callable<T>> tasks) 
Executes the given tasks, returning a list of Futures holding their status and results when all complete.执行给定的任务,当所有任务都完成时,返回一个保存其状态和结果的未来列表。
<T> List<Future<T>>invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) 
Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first.执行给定的任务,当所有任务完成或超时过期(以先发生的为准)时,返回一个保存其状态和结果的未来列表。
<T> TinvokeAny(Collection<? extends Callable<T>> tasks) 
Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do.执行给定的任务,返回已成功完成的任务的结果(即,不引发异常),如果有的话。
<T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) 
Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses.执行给定的任务,如果在给定的超时时间过去之前执行任何操作,则返回已成功完成的任务的结果(即,不引发异常)。
booleanisShutdown() 
Returns true if this executor has been shut down.返回true;如果此执行器已关闭。
booleanisTerminated() 
Returns true if all tasks have completed following shut down.如果关闭后所有任务都已完成,则返回true
voidshutdown() 
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.启动有序关闭,其中执行以前提交的任务,但不接受新任务。
List<Runnable>shutdownNow() 
Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.尝试停止所有正在执行的任务,停止正在等待的任务的处理,并返回正在等待执行的任务的列表。
<T> Future<T>submit(Callable<T> task) 
Submits a value-returning task for execution and returns a Future representing the pending results of the task.提交一个返回值的任务以供执行,并返回一个表示任务挂起结果的Future
Future<?>submit(Runnable task) 
Submits a Runnable task for execution and returns a Future representing that task.提交可运行任务以执行,并返回表示该任务的Future
<T> Future<T>submit(Runnable task, T result) 
Submits a Runnable task for execution and returns a Future representing that task.提交可运行任务以执行,并返回表示该任务的Future

 

二.实现一个功能案例

案例功能实现:使用多线程对指定一系列机器做ping网络检测,并输出检测结果。

基本思路:使用锁synchronized和volatile关键字来确保多线程环境中的数据操作安全,并结合java多线程api,实现快速的对一组机器做ping网络检测,并输出检测结果。

java实现

package simple.callback.network;import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;/*** @program: psimple* @description:java多线程安全与关键字volatile/synchronized,Executors、ExecutorService* @author: Mr.Wang* @create: 2021年 01月 18日 星期一 22:55:22 CST**/
public class PingNetworkV6 {public static void main(String[] args) throws InterruptedException {long start = System.currentTimeMillis();int ips = PingNetworkThread.allIp.size();int threadnum = 15;//定长线程池线程数量int runablecounts = 13;//待执行的任务数量ExecutorService executorService = Executors.newFixedThreadPool(threadnum);for (int i = 0; i < runablecounts; i++)executorService.submit(new PingNetworkThread());executorService.shutdown();while (true) {if (executorService.isTerminated() == true) {long end = System.currentTimeMillis();System.out.println();System.out.println("[任务执行完毕]:" + ips + "个ip检测");System.out.println("[ping成功ip串]:" + PingNetworkThread.ipsOK);System.out.println("[ping失败ip串]:" + PingNetworkThread.ipsNO);System.out.println(threadnum + "个线程异步执行" + runablecounts + "个任务,耗时:" + (end - start) + "ms");break;}}}}class PingNetworkThread extends Thread {public volatile static Queue<String> allIp;public volatile static String ipsOK = "";public volatile static String ipsNO = "";private String currentIp = "";static {allIp = new LinkedList<String>();//已知ping的通的allIp.offer("127.0.0.1");allIp.offer("127.0.0.2");//已知ping不通的allIp.offer("192.168.0.0");allIp.offer("192.168.1.109");//再往队列新加几条ping不通的ipallIp.offer("192.168.1.110");allIp.offer("192.168.1.111");allIp.offer("192.168.1.112");allIp.offer("192.168.1.113");allIp.offer("192.168.1.114");allIp.offer("192.168.1.115");allIp.offer("192.168.1.116");allIp.offer("192.168.1.117");allIp.offer("192.168.1.118");allIp.offer("192.168.1.119");allIp.offer("192.168.1.120");/*for (int i = 120; i < 120 + 100; i++) {allIp.offer("192.168.1." + i);}*/}public PingNetworkThread() {}public PingNetworkThread(Queue<String> ipqueue) {allIp = ipqueue;}public String getIp() {String ip = null;synchronized (allIp) {ip = allIp.poll();}return ip;}@Overridepublic void run() {while ((currentIp = getIp()) != null) {try {//System.out.println(Thread.currentThread().getName() + ",currentIp:" + currentIp);if (PingUtils.ping(currentIp, 1)) {//把数值改小,包的个数synchronized (PingNetworkThread.class) {ipsOK += currentIp + ",";System.out.println(Thread.currentThread().getName() + ",ping成功ip串:" + ipsOK);}} else {synchronized (PingNetworkThread.class) {ipsNO += currentIp + ",";System.out.println(Thread.currentThread().getName() + ",ping失败ip串:" + ipsNO);}}} catch (Exception e) {e.printStackTrace();}}}
}

程序执行输出:

pool-1-thread-2,ping成功ip串:127.0.0.2,
pool-1-thread-1,ping成功ip串:127.0.0.2,127.0.0.1,
pool-1-thread-9,ping失败ip串:192.168.1.114,
pool-1-thread-3,ping失败ip串:192.168.1.114,192.168.0.0,
pool-1-thread-4,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,
pool-1-thread-10,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,
pool-1-thread-11,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,192.168.1.116,
pool-1-thread-6,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,192.168.1.116,192.168.1.111,
pool-1-thread-5,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,192.168.1.116,192.168.1.111,192.168.1.110,
pool-1-thread-13,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,192.168.1.116,192.168.1.111,192.168.1.110,192.168.1.118,
pool-1-thread-7,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,192.168.1.116,192.168.1.111,192.168.1.110,192.168.1.118,192.168.1.112,
pool-1-thread-12,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,192.168.1.116,192.168.1.111,192.168.1.110,192.168.1.118,192.168.1.112,192.168.1.117,
pool-1-thread-8,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,192.168.1.116,192.168.1.111,192.168.1.110,192.168.1.118,192.168.1.112,192.168.1.117,192.168.1.113,
pool-1-thread-2,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,192.168.1.116,192.168.1.111,192.168.1.110,192.168.1.118,192.168.1.112,192.168.1.117,192.168.1.113,192.168.1.119,
pool-1-thread-1,ping失败ip串:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,192.168.1.116,192.168.1.111,192.168.1.110,192.168.1.118,192.168.1.112,192.168.1.117,192.168.1.113,192.168.1.119,192.168.1.120,[任务执行完毕]:15个ip检测
[ping成功ip串]:127.0.0.2,127.0.0.1,
[ping失败ip串]:192.168.1.114,192.168.0.0,192.168.1.109,192.168.1.115,192.168.1.116,192.168.1.111,192.168.1.110,192.168.1.118,192.168.1.112,192.168.1.117,192.168.1.113,192.168.1.119,192.168.1.120,
15个线程异步执行13个任务,耗时:1123msProcess finished with exit code 0

功能案例完整源码下载:

链接一: pingsource_v2.6.tar.gz

链接二: https://pan.baidu.com/s/1-OnZ4BGubOC5O-BaeKp_aA 提取码: ezpg

http://www.lbrq.cn/news/1260577.html

相关文章:

  • 洛阳网站设计哪家专业/推广引流最快的方法
  • 做网站购买备案域名/品牌推广方案包括哪些
  • 熊岳网站在哪做/长沙网络公关公司
  • 网站备案怎么在工信部信息核验/郑州网站
  • 做技术分享网站有哪些/百度怎么做推广和宣传
  • 佛山 做网站公司/网站制作app
  • 企业信用查询平台/二十个优化
  • 视频解析网站怎么做/网络推广哪个好
  • 做网站小程序多少钱/b站推广2023
  • 网站建设模版/技师培训
  • 项链seo关键词/网站seo综合查询
  • 天眼查网站建设公司/网络推广工作好做不
  • 东莞注册公司需要什么资料/seo外链建设方法
  • 做网站为什么要服务器/怎么登录百度app
  • 动态网站系统的5个组成部分/360点睛实效平台推广
  • 专业网站制作公司咨询/北京软件开发公司
  • 长垣网站建设/百度搜索引擎官网入口
  • 安卓app开发工具/苏州网站关键字优化
  • 花店网站建设环境分析/互联网营销师是什么
  • 邢台网站制作公司哪家专业/百度自动驾驶技术
  • 百度外卖网站建设与维护方法/百度大数据中心
  • 养老院微网站建设方案/天津优化代理
  • 玉林做网站/网站排名靠前方法
  • 专业做网站的页面设计/网文网站排名
  • 广东省政府网站建设/1个百度指数代表多少搜索
  • 有什么做家常菜的网站/一般的电脑培训班要多少钱
  • 有做国际网站生意吗/移动端关键词优化
  • 视频拍摄设备/网站内部seo优化包括
  • 会员卡管理系统软件哪个好用/盐城seo优化
  • 建设银行违法网站/郑州网络推广大包
  • k8s集群
  • 案例介绍|JSON数据格式的转换|pyecharts模块简介
  • C的数据类型与变量
  • 【C++ 初级工程师面试--4】形参带默认值的函数,特点,效率,注意事项
  • LangGraph认知篇-Command函数
  • 我的世界进阶模组教程——物品(2)