生成的静态网页: https://www.xiaogenban1993.com
sunwu51 / notebook Goto Github PK
View Code? Open in Web Editor NEWmy note
Home Page: https://xiaogenban1993.com
my note
Home Page: https://xiaogenban1993.com
生成的静态网页: https://www.xiaogenban1993.com
有时候我们需要快速的启动一个http的服务器,将当前目录的文件放映射出去。
像nginx或者apache httpd等服务器,安装和配置都是很麻烦的,使用下面的几种方式则可以更快的启动服务。
如果机器上刚好安装了python、php、ruby等,那么就可以一行指令搞定当前目录啦。
# python 3.x(2.x的就不说了)
python3 -m http.server
# php
php -S localhost:8080
# ruby
ruby -run -e httpd . -p 8080
或者稍微麻烦一点,一些编程语言的库也能做到,例如nodejs
# 安装http-server
npm i -g http-server
# 运行
http-server -p 8080
可以自己写一个提供http服务的程序,然后将程序打包为二进制文件,放到互联网上,这样要用到的时候,就去下载下来,直接运行二进制文件就可以啦,不需要安装任何语言环境了。我好久之前基于gin写了一个这样的小工具,如果是linux下就可以这样使用。
# 下载二进制文件(mac则将linux改成mac,win则改为win并加.exe后缀)
wget -O server https://github.com/sunwu51/Tool/releases/download/2/server_linux_amd64
# 增加可执行权限
chmod +x server
# 运行
./server -port 8080 -assets .
https://github.com/sunwu51/notebook/blob/master/20.01/numpy101.ipynb
在这里面的第15题
15 取a,b两等长数组中较大值为新数组
in: a = [4,6,1,7,9]
b=[1,6,9,2,7]
现有的答案是
a = [4,6,1,7,9]
b=[1,6,9,2,7]
def max(x,y):
if x>y:
return x
else:
return y
max(a,b)
返回值
[4, 6, 1, 7, 9]
感觉并不work啊, 不应该是[4,6,9,7,9]
么
例如这样场景:文件每一行是个url,记录有多少个返回结果是200成功,有多少是其他状态码。
注意:
一个重点是线程池该如何定制参数,另一个重点则是如何知道所有任务结束了。
用newCacheThreadPool
或者newFixedThreadPool
都是不合适的,前者会一直创建新线程直到创建int最大值个线程,之后再有线程就会抛异常然后程序就炸了。后者可以创建固定的线程数,但是如果超过这个数就会扔到队列LinkedBlockingQueue,这个队列也是最大只能int最大值个,仍有上面的问题。
在当前场景下,我们可以对newCacheThreadPool
或者newFixedThreadPool
进行结合,模仿后者创建固定个数的线程,但是队列我们采用前者的空队列,这时候会在到达指定个数后reject,对于reject策略我们可以使用ThreadPoolExecutor.CallerRunsPolicy
。这样在超出线程个数的时候,就会由创建线程的线程(我们这里就是主线程)来执行当前任务,因为"生产者"去帮忙"消费"了,所以生产任务就暂停了,这样起到一个负反馈的作用,使在工作的线程使用不超过101个,如下。
ThreadPoolExecutor threadPool =
new ThreadPoolExecutor(100,100,0, TimeUnit.SECONDS,new SynchronousQueue<>(),new ThreadPoolExecutor.CallerRunsPolicy())
即整个程序大概是这样:
// 用于记录200状态码,和其他状态码的数量
AtomicInteger okCount = new AtomicInteger();
AtomicInteger otherCount = new AtomicInteger();
String line = null;
while( (line = readLine(file)) !=null){
String tmp = line;
threadPool.execute(()->{
// 对每一行进行处理
process(tmp,okCount,otherCount );
});
}
System.out.println("ok个数"+okCount.get()+" 不ok的个数"+otherCount);
但打印结果这一行可能有问题,例如最后100个url的请求时间超级长,这100个线程创建完之后,就到了打印这一行了但是还没执行完,这样结果就是错的。常规思路下,计数器CountDownLatch
是最佳选择,但是当前题目无法获知文件大小,而计数器必须一开始就指定计数的大小。这怎么解决呢?
当然思路是很多的,例如最简单的能想到的,就是在打印前判断线程池中活跃线程>0,就sleep。
while(threadPool.getActiveCount()>0){
Thread.sleep(1000);
}
这样做不是特别的好,首先是这个getActiveCount拿到的个数是个大约的值,不保证准确,其次sleep的时长如何设定是恰当的有待商榷。我比较喜欢的做法是利用相位器Phaser
。代码如下,有注释,但是可能也不太好理解,建议看下相位器那期视频。
AtomicInteger okCount = new AtomicInteger();
AtomicInteger otherCount = new AtomicInteger();
String line = null;
// 创建数为1的相位器,不设置为0,是因为如果相位器总数从1减到0时,会导致阶段变为负数。1是个保护屏障。
Phaser phaser = new Phaser(1);
while( (line = readLine(file)) !=null){
String tmp = line;
// 每一行内容读出来时相位器总数+1
phaser.register();
threadPool.execute(()->{
// 对每一行进行处理
process(tmp,okCount,otherCount );
// 处理完一行相位器总数-1
phaser.arriveAndDeregister();
});
}
// 因为初值设为1,所以最后需要arrive一次
phaser.arrive();
phaser.awaitAdvance(0);
System.out.println("ok个数"+okCount.get()+" 不ok的个数"+otherCount);
java总遇到对象为null,此时不论是调用方法还是属性都会产生空指针异常
。需要写if(obj != null)
这样的判断。
我们写java的时候,经常遇到以下几个问题。
1 对于字符串除了判null,还要判空串。
2 对于集合对象例如list、set还要判断是否是0个元素的空集合。
3 判断两个对象是否equals的时候,一定不为null的最好放在前面,例如不要写a.equals("字符串")
,而要写"字符串".equals(a)
,但如果俩对象都不知道是不是null可能就需要这样
if(a != null && a.equals(b)){
xxx
}
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
例如
// 1 如果字符串str是null,或者空字符串,则返回true
StringUtils.isEmpty(str);
// 2 如果str是null,空字符串,或只含有空格的字符串,则返回true
StringUtils.isBlank(str);
// 3 如果list是null或者是空的则返回true,可以接收list set等集合类
CollectionUtils.isEmpty(list);
// 4 两个对象都不为null且equals,或者俩对象都是null,则返回true,否则是false。
// Objects不是第三方包,是java7之后引入到jdk中的,可以直接使用,java7之前也可以使用guava中的Objects,用法一样。
Objects.equals(obj1, obj2);
我们可以使用工具类封好的判断方法,来简化变量的判断相等。当然仔细研究下这些工具库,就会发现除了判断相等还封装了很多常用的方法。
这样的工具包其实很多,就单说apache commons
除了上面提到的lang
,collections
还有IO
,BeanUtils
,logging
,CSV
等等参考,此外谷歌的guava
也有很多类似的工具类的封装。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.