java中对于大量的比较耗时的任务多采用多线程对方式对任务进行处理,同时由于进程和线程 本身是通过宿主机OS进行管理的,当在cpu核数较少或线程分配不当 会导致多线程的效果不佳的事常有发生
代码片段:
//处理器核心数
int processor = Runtime.getRuntime().availableProcessors();
//XSSFWorkbook 一次只能写入六万多条数据,所以这里最好使用SXSSFWorkbook
SXSSFWorkbook workBook = new SXSSFWorkbook();
//创建格式
CellStyle style = workBook.createCellStyle();
//居中格式
style.setAlignment(HorizontalAlignment.CENTER);
//手工创建线程池
ExecutorService executorService = new ThreadPoolExecutor(processor, processor, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque(),
new ThreadFactoryBuilder().setNameFormat("poi-task-%d").build());
//计数器 等待线程池中的线程执行完毕
CountDownLatch countDownLatch = new CountDownLatch(processor);
for (int i = 0; i < processor; i++) {
int sheetId = i;
//放入线程池中
executorService.execute(() -> createSheet(workBook, style,sheetId, countDownLatch));
}
try {
//等待所有线程执行完毕
countDownLatch.await();
executorService.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
代码片段:
import (
"fmt"
"sync"
)
func says(s string, gw *sync.WaitGroup) {
for i := 0; i < 5; i++ {
fmt.Println(">>> ", s)
}
gw.Done()
}
func main() {
var gw sync.WaitGroup
gw.Add(1)
go says("Hello s", &gw)
gw.Wait()
}
java对于函数值对传递采取对是值传递的方式,对于基本数据类型:传递前后值所在栈的位置是不一致的(也就是被拷贝了一份) 对于非基本数据类型:虽然也会做拷贝,但实际上这前后的对象引用的是同一内存位置的值,这就造成了"引用传递的假象"
代码片段:
public class TransParams {
public static void main(String[] args){
Person p = new Person();
p.setAge(99);
p.setName("Lisa");
System.out.println(p.getAge());
System.out.println(p);
System.out.println("======>split<=====");
TransParams tp = new TransParams();
tp.setValue(p);
System.out.println(p.getAge());
System.out.println(p);
}
public void setValue(Person p){
p.setAge(19);
}
}
class Person {
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行结果:
99
com.task.charset.Person@7e0b37bc
======>split<=====
19
com.task.charset.Person@7e0b37bc
import "fmt"
func main() {
var s1 []string
fmt.Println("拷贝传递前>", s1)
tr01(s1)
fmt.Println("拷贝传递后>", s1)
fmt.Println("=====><=====")
var s2 []string
fmt.Println("指针传递前>", s2)
tr02(&s2)
fmt.Println("指针传递后>", s2)
}
func tr01(m []string) {
m = append(m, "youth01")
}
func tr02(mm *[]string) {
*mm = append(*mm, "youth02")
}
输出结果:
拷贝传递前> []
拷贝传递后> []
=====><=====
指针传递前> []
指针传递后> [youth02]
在java8之前jdk仅提供了Date类型的格式化,对应的日期处理类是SimpleDateFormat, 在java8至java8之后Oracle提供了LocalDate与LocalDateTime的两种日期格式,对应的日期处理类是DateTimeFormat
代码片段:
public class Format2LocalDate {
private static final Logger LOG = LoggerFactory.getLogger(Format2LocalDate.class);
private static final DateTimeFormatter DATE_FORMAT_SHORT = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
@Test
public void transDate(){
this.parse();
this.format();
LOG.info(".....................");
this.parseD();
this.formatD();
}
public void parse(){
String str = "20190116 12:12:22";
Date today = Date.from(LocalDateTime.parse(str,DATE_FORMAT_SHORT).atZone(DateUtil.CHINA_ZONE_ID).toInstant());
LOG.info("转换结果为> {}",today);
}
public void format(){
LocalDateTime ldt = LocalDateTime.now();
LOG.info("格式化字符串> {}",ldt.format(DATE_FORMAT_SHORT));
}
public final static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public void parseD(){
String dateStr = "2019-01-01 12:22:33";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
Date date = null;
try {
date = simpleDateFormat.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
LOG.info("转换结果为> {}",date);
}
public void formatD(){
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
LOG.info("格式化结果为> {}",simpleDateFormat.format(date));
}
}
输出结果为:
转换结果为> Wed Jan 16 12:12:22 CST 2019
格式化字符串> 20190313 21:20:23
.....................
转换结果为> Tue Jan 01 12:22:33 CST 2019
格式化结果为> 2019-03-13 21:20:23
/**
官方定义的不可更改
*/
const DATE_FORMAT string = "2006-01-02 15:04:05"
func main() {
parse()
format()
}
func parse() {
tm := time.Now()
strs := tm.Format(DATE_FORMAT)
fmt.Println("日期转换为字符串> ", strs)
}
func format() {
tm, _ := time.Parse(DATE_FORMAT, "2019-01-01 12:12:12")
fmt.Println("字符串转换为日期> ", tm)
}
运行结果:
日期转换为字符串> 2019-03-13 21:29:30
字符串转换为日期> 2019-01-01 12:12:12 +0000 UTC
//加减乘除都出现了对应的精度问题
public class MathCalcul {
private static final Logger LOG = LoggerFactory.getLogger(MathCalcul.class);
@Test
public void calcul(){
LOG.info("加: {}",0.1 + 0.2);
LOG.info("减: {}",1.1 - 0.11);
LOG.info("乘: {}",1.13 * 100);
LOG.info("除: {}",100.13 / 100);
}
}
输出结果:
加: 0.30000000000000004
减: 0.9900000000000001
乘: 112.99999999999999
除: 1.0012999999999999
func main() {
fmt.Println("加: ", 0.1+0.2)
fmt.Println("减: ", 1.1-0.11)
fmt.Println("乘: ", 1.13*100)
fmt.Println("除: ", 100.13/100)
}
输出结果:
加: 0.3
减: 0.99
乘: 113
除: 1.0013
class MyServlet extends HttpServlet{
private static final ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(405, msg);
} else {
resp.sendError(400, msg);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_post_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(405, msg);
} else {
resp.sendError(400, msg);
}
}
}
import (
"fmt"
"net/http"
)
func index_handle(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Whoa,Go is cool!")
}
func main() {
http.HandleFunc("/", index_handle)
http.ListenAndServe(":8000", nil)
}
//静态
public static String str = "hello";
//常量
public final String str2 = "hello2";
//不可变量(初始化后不可重新赋值)
public static final String str3 = "hello3";
const str string = "hello"
__本章就到这里吧,敬请期待下一讲。(^_^)__