Ai
1 Star 0 Fork 59

Yangjianwei/java-performance

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Server.java 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
xiandafu 提交于 2019-11-12 14:15 +08:00 . ”test“
package com.ibeetl.code.ch03;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
/**
* CountDownLatch使用例子
* @author 公众号 java系统优化
*/
public class Server {
enum Status{NOMARL,ERROR}
ConcurrentHashMap<String,Status> status = new ConcurrentHashMap<>();
CountDownLatch latch;
public Server(){
}
public void start() throws Exception{
String[] urls = new String[]{"192.168.1.13","192.168.1.14"};
check(urls);
}
public void check(String[] urls) throws Exception{
latch = new CountDownLatch(urls.length);
for(String url:urls){
Thread t = new Thread(new ServiceCheck(this,url),"check-"+url);
t.start();
}
latch.await();
System.out.println(status);
}
public static void main(String[] args) throws Exception{
Server server = new Server();
server.start();
}
class ServiceCheck implements Runnable{
String url = null;
Server server;
public ServiceCheck(Server server,String url){
this.server = server;
this.url = url;
}
@Override
public void run() {
check();
server.latch.countDown();
}
private void check(){
try{
//模拟检测
Thread.sleep(100);
server.status.put(url,Status.NOMARL);
System.out.println("check "+url);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/higher/java-performance.git
git@gitee.com:higher/java-performance.git
higher
java-performance
java-performance
master

搜索帮助