Skip to content

Commit 60af82b

Browse files
committed
🍱 add java8 growing code
1 parent e52584e commit 60af82b

File tree

9 files changed

+202
-0
lines changed

9 files changed

+202
-0
lines changed

java8-growing/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>learn-java8</artifactId>
7+
<groupId>io.github.biezhi</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>java8-growing</artifactId>
13+
14+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.biezhi.java8.growing.jdk5;
2+
3+
/**
4+
* 自动装箱、拆箱
5+
*/
6+
public class AutoBoxing {
7+
8+
public static void main(String[] args) {
9+
int a = new Integer(66);
10+
Integer b = 18;
11+
12+
Boolean flag = true;
13+
boolean isBug = Boolean.FALSE;
14+
15+
}
16+
17+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package io.github.biezhi.java8.growing.jdk5;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Queue;
6+
import java.util.concurrent.*;
7+
import java.util.concurrent.locks.Condition;
8+
import java.util.concurrent.locks.Lock;
9+
import java.util.concurrent.locks.ReentrantLock;
10+
11+
/**
12+
* 并发库
13+
*/
14+
public class Concurrent {
15+
16+
public void lock() {
17+
Lock lock = new ReentrantLock();
18+
lock.lock();
19+
try {
20+
System.out.println("hello world");
21+
} finally {
22+
lock.unlock();
23+
}
24+
}
25+
26+
public void condition() throws InterruptedException {
27+
Lock lock = new ReentrantLock();
28+
Condition condition = lock.newCondition();
29+
// do something
30+
condition.await(10, TimeUnit.SECONDS);
31+
System.out.println("Get result.");
32+
}
33+
34+
public void executorService() {
35+
ExecutorService executorService = Executors.newFixedThreadPool(3);
36+
executorService.submit(new Runnable() {
37+
@Override
38+
public void run() {
39+
System.out.println("Task is running.");
40+
}
41+
});
42+
}
43+
44+
public void blockingDeque() {
45+
Queue<Integer> blockingDeque = new ArrayBlockingQueue<>(20);
46+
blockingDeque.add(1);
47+
blockingDeque.add(2);
48+
blockingDeque.add(3);
49+
50+
blockingDeque.peek();
51+
}
52+
53+
public void concurrentHashMap() {
54+
Map<String, Integer> concurrentHashMap = new ConcurrentHashMap<>();
55+
concurrentHashMap.put("Hello", 1);
56+
concurrentHashMap.put("World", 2);
57+
58+
System.out.println(concurrentHashMap.get("Hello"));
59+
}
60+
61+
public void copyOnWriteList() {
62+
List<String> copyOnWriteList = new CopyOnWriteArrayList<>();
63+
copyOnWriteList.add("a");
64+
copyOnWriteList.add("b");
65+
copyOnWriteList.add("c");
66+
67+
System.out.println(copyOnWriteList.size());
68+
}
69+
70+
public void semaphore() {
71+
Semaphore semaphore = new Semaphore(3);
72+
try {
73+
semaphore.acquire();
74+
System.out.println(Thread.currentThread().getName() + " is working");
75+
Thread.sleep(1000);
76+
semaphore.release();
77+
System.out.println(Thread.currentThread().getName() + " is over");
78+
} catch (InterruptedException e) {
79+
}
80+
}
81+
82+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.github.biezhi.java8.growing.jdk5;
2+
3+
/**
4+
* 枚举
5+
*/
6+
public enum EnumDemo {
7+
8+
RED, GREEN, YELLOW
9+
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.biezhi.java8.growing.jdk5;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* for each
8+
*/
9+
public class ForEach {
10+
11+
public static void main(String[] args) {
12+
13+
int[] arr = {1, 4, 5, 7};
14+
15+
for (int i : arr) {
16+
System.out.println(i);
17+
}
18+
19+
List<String> names = Arrays.asList("王爵nice", "Gay冰", "A*熊");
20+
for (String name : names) {
21+
System.out.println(name);
22+
}
23+
}
24+
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.biezhi.java8.growing.jdk5;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 泛型
8+
*/
9+
public class Generic<T> {
10+
11+
public static void main(String[] args) {
12+
13+
Map<String, Integer> map = new HashMap<>();
14+
15+
Generic<Long> generic = new Generic<>();
16+
17+
}
18+
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.biezhi.java8.growing.jdk5;
2+
3+
import static java.lang.System.out;
4+
5+
/**
6+
* 静态导入
7+
*/
8+
public class StaticImport {
9+
10+
public static void main(String[] args) {
11+
out.println("Hi let learn java 8.");
12+
}
13+
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.biezhi.java8.growing.jdk5;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* 变长参数
8+
*/
9+
public class VarArgs {
10+
11+
public static List<String> asList(String[] names){
12+
return Arrays.asList(names);
13+
}
14+
15+
public static void main(String[] args) {
16+
List<String> hello = Arrays.asList("王爵nice", "Gay冰", "A*熊");
17+
18+
}
19+
20+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<module>java8-best-practice</module>
2020
<module>java8-proper</module>
2121
<module>java8-concurrent</module>
22+
<module>java8-growing</module>
2223
</modules>
2324

2425
<dependencies>

0 commit comments

Comments
 (0)