Skip to content

Commit f1fa8de

Browse files
author
yongjie.zhang@ucarinc.com
committed
stream操作、optional、jdk1.8新时间类
1 parent 559faa3 commit f1fa8de

File tree

24 files changed

+1369
-1
lines changed

24 files changed

+1369
-1
lines changed

java-fork-join/java-fork-join.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package fork_join;
2+
3+
4+
import java.io.FileOutputStream;
5+
import java.lang.invoke.MethodHandle;
6+
import java.lang.invoke.MethodHandles;
7+
import java.lang.invoke.MethodType;
8+
import java.nio.channels.ServerSocketChannel;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.concurrent.ExecutionException;
12+
import java.util.concurrent.ForkJoinPool;
13+
import java.util.concurrent.ForkJoinTask;
14+
import java.util.concurrent.RecursiveTask;
15+
16+
/**
17+
* @author : 张勇杰
18+
* @date : 2019/9/23 16:19
19+
* @Version : v1.0
20+
* @description
21+
**/
22+
public class Test {
23+
private static int threshold = 100;
24+
private static int sum = 0;
25+
static class MyRecursive extends RecursiveTask<Long>{
26+
27+
Long sum = Long.valueOf(0);
28+
private int left;
29+
private int right;
30+
31+
public MyRecursive(int left, int right) {
32+
this.left = left;
33+
this.right = right;
34+
}
35+
36+
@Override
37+
protected Long compute() {
38+
if(right - left <= threshold){
39+
for(int i = left; i <= right ; i++){
40+
sum += i;
41+
}
42+
}else{
43+
int mid = (left + right) / 2;
44+
MyRecursive left = new MyRecursive(this.left,mid);
45+
left.fork();
46+
MyRecursive right = new MyRecursive(mid,this.right);
47+
48+
right.fork();
49+
50+
Long l = left.join();
51+
Long r = right.join();
52+
53+
sum = l + r;
54+
}
55+
return sum;
56+
}
57+
}
58+
59+
public static void main(String[] args) throws Throwable {
60+
// ForkJoinPool commonpool = new ForkJoinPool();
61+
// MyRecursive myRecursive = new MyRecursive(1,100);
62+
// ForkJoinTask<Long> result = commonpool.submit(myRecursive);
63+
// System.out.println(result.get());
64+
// commonpool.shutdown();
65+
// ForkJoinPool commonpool = new ForkJoinPool();
66+
// List<User> userList = new ArrayList<>();
67+
//
68+
// for(int i = 0 ;i< 100;i++){
69+
// userList.add(new User());
70+
// }
71+
// UserRecursiveAction userRecursiveAction = new UserRecursiveAction(userList);
72+
// commonpool.submit(userRecursiveAction);
73+
// System.out.println(userList);
74+
// commonpool.shutdown();
75+
// System.out.println(sum(100));
76+
// fanxing<String,Integer> fanxin = new fanxing<>("1",1);
77+
// Object object = new String("11");
78+
//// getPrintlnMH(object).invokeExact("avcc");
79+
// FileOutputStream out = new FileOutputStream("D:\\a.txt");
80+
// out.write(97);
81+
// out.close();
82+
// List<Integer> list = new ArrayList<>();
83+
// list.add(1);
84+
// list.add(2);
85+
// list.add(1);
86+
// list.add(3);
87+
// list.add(4);
88+
// list.add(5);
89+
// list.add(6);
90+
// list.add(7);
91+
// list.add(8);
92+
// for(Integer temp : list){
93+
// if(temp.equals(1)){
94+
// list.remove(temp);
95+
// }
96+
//// list.removeAll(list);
97+
// }
98+
System.out.println(TestStatic.date);
99+
System.out.println(TestStatic.date);
100+
System.out.println(TestStatic.date);
101+
}
102+
103+
public static MethodHandle getPrintlnMH(Object reveiver) throws NoSuchMethodException, IllegalAccessException {
104+
MethodType mt = MethodType.methodType(void.class,String.class);
105+
return MethodHandles.lookup().findVirtual(reveiver.getClass(),"println",mt).bindTo(reveiver);
106+
}
107+
108+
public static int sum(int n){
109+
if(n == 1){
110+
return 1;
111+
}
112+
return n+sum(n-1);
113+
}
114+
static class fanxing<S,D>{
115+
S data;
116+
D data2;
117+
118+
public fanxing(S data, D data2) {
119+
this.data = data;
120+
this.data2 = data2;
121+
}
122+
123+
}
124+
125+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package fork_join;
2+
3+
import java.util.Date;
4+
5+
/**
6+
* @author : 张勇杰
7+
* @date : 2019/10/14 17:08
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class TestStatic {
12+
static Date date = new Date();
13+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package fork_join;
2+
3+
/**
4+
* @author : 张勇杰
5+
* @date : 2019/9/23 17:51
6+
* @Version : v1.0
7+
* @description
8+
**/
9+
public class User {
10+
private String name;
11+
private int age;
12+
13+
public User() {
14+
}
15+
16+
public User(String name, int age) {
17+
this.name = name;
18+
this.age = age;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
public int getAge() {
30+
return age;
31+
}
32+
33+
public void setAge(int age) {
34+
this.age = age;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "User{" +
40+
"name='" + name + '\'' +
41+
", age=" + age +
42+
'}';
43+
}
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package fork_join;
2+
3+
import java.util.List;
4+
import java.util.concurrent.RecursiveAction;
5+
6+
/**
7+
* @author : 张勇杰
8+
* @date : 2019/9/23 17:52
9+
* @Version : v1.0
10+
* @description
11+
**/
12+
public class UserRecursiveAction extends RecursiveAction {
13+
14+
private List<User> userList;
15+
16+
private int start,end;
17+
18+
public UserRecursiveAction(List<User> userList) {
19+
this.userList = userList;
20+
// this.start = start;
21+
// this.end = end;
22+
}
23+
24+
@Override
25+
protected void compute() {
26+
if(userList.size() <= 10){
27+
for(User u :userList){
28+
u.setAge(1);
29+
u.setName("zhang");
30+
}
31+
}else{
32+
int mid = userList.size()/2;
33+
UserRecursiveAction left = new UserRecursiveAction(userList.subList(0,mid));
34+
UserRecursiveAction right = new UserRecursiveAction(userList.subList(mid,userList.size()));
35+
left.fork();
36+
right.fork();
37+
38+
39+
}
40+
41+
}
42+
}

java-nio/src/12.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
hello world!!!!@####@@
2+
hello world!!!!@####@@
3+
hello world!!!!@####@@
4+
hello world!!!!@####@@
5+
hello world!!!!@####@@
6+
hello world!!!!@####@@
7+
hello world!!!!@####@@
8+
hello world!!!!@####@@
9+
hello world!!!!@####@@
10+
hello world!!!!@####@@
11+
hello world!!!!@####@@
12+
hello world!!!!@####@@
13+
hello world!!!!@####@@
14+
hello world!!!!@####@@
15+
hello world!!!!@####@@
16+
hello world!!!!@####@@
17+
hello world!!!!@####@@
18+
hello world!!!!@####@@
19+
hello world!!!!@####@@
20+
hello world!!!!@####@@
21+
hello world!!!!@####@@
22+
hello world!!!!@####@@
23+
hello world!!!!@####@@
24+
hello world!!!!@####@@
25+
hello world!!!!@####@@
26+
hello world!!!!@####@@
27+
hello world!!!!@####@@
28+
hello world!!!!@####@@
29+
hello world!!!!@####@@
30+
hello world!!!!@####@@
31+
hello world!!!!@####@@
32+
hello world!!!!@####@@
33+
hello world!!!!@####@@
34+
hello world!!!!@####@@
35+
hello world!!!!@####@@
36+
hello world!!!!@####@@
37+
hello world!!!!@####@@
38+
hello world!!!!@####@@
39+
hello world!!!!@####@@
40+
hello world!!!!@####@@
41+
hello world!!!!@####@@
42+
hello world!!!!@####@@
43+
hello world!!!!@####@@
44+
hello world!!!!@####@@
45+
hello world!!!!@####@@
46+
hello world!!!!@####@@
47+
hello world!!!!@####@@
48+
hello world!!!!@####@@
49+
hello world!!!!@####@@
50+
hello world!!!!@####@@
51+
hello world!!!!@####@@
52+
hello world!!!!@####@@
53+
hello world!!!!@####@@
54+
hello world!!!!@####@@
55+
hello world!!!!@####@@
56+
hello world!!!!@####@@
57+
hello world!!!!@####@@
58+
hello world!!!!@####@@
59+
hello world!!!!@####@@
60+
hello world!!!!@####@@
61+
hello world!!!!@####@@
62+
hello world!!!!@####@@
63+
hello world!!!!@####@@
64+
hello world!!!!@####@@
65+
hello world!!!!@####@@
66+
hello world!!!!@####@@
67+
hello world!!!!@####@@
68+
hello world!!!!@####@@
69+
hello world!!!!@####@@
70+
hello world!!!!@####@@
71+
hello world!!!!@####@@
72+
hello world!!!!@####@@
73+
hello world!!!!@####@@
74+
hello world!!!!@####@@
75+
hello world!!!!@####@@
76+
hello world!!!!@####@@
77+
hello world!!!!@####@@
78+
hello world!!!!@####@@
79+
hello world!!!!@####@@
80+
hello world!!!!@####@@
81+
hello world!!!!@####@@
82+
hello world!!!!@####@@
83+
hello world!!!!@####@@
84+
hello world!!!!@####@@
85+
hello world!!!!@####@@
86+
hello world!!!!@####@@
87+
hello world!!!!@####@@
88+
hello world!!!!@####@@
89+
hello world!!!!@####@@
90+
hello world!!!!@####@@
91+
hello world!!!!@####@@
92+
hello world!!!!@####@@
93+
hello world!!!!@####@@
94+
hello world!!!!@####@@
95+
hello world!!!!@####@@
96+
hello world!!!!@####@@
97+
hello world!!!!@####@@
98+
hello world!!!!@####@@
99+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.asher.nio;
2+
3+
/**
4+
* @author : 张勇杰
5+
* @date : 2019/8/28 13:20
6+
* @Version : v1.0
7+
* @description
8+
* 一、使用NIO完成网络通信的三个核心
9+
* 1.通道(Channel):负责连接
10+
* java.nio.channels.Channel接口:
11+
* |--SelectableChannel
12+
* |--SocketChannel
13+
* |--ServerSocketChannel
14+
* |--DatagramChannel
15+
*
16+
* |--Pipe.SinkChannel
17+
* |--Pipe.SourceChannel
18+
*
19+
* 2.缓冲区(Buffer):负责数据存取
20+
*
21+
* 3.选择器(Selector):是SelectableChannel的多路复用器,用于监控SelectableChannel的IO状况
22+
*
23+
**/
24+
public class BlockingNIOTest {
25+
public static void main(String[] args) {
26+
//1.
27+
}
28+
}

0 commit comments

Comments
 (0)