Skip to content

Commit 49c6fd7

Browse files
committed
Merge branch 'main' of https://github.com/codingCoder6/JavaCourseCodes into main
2 parents f165a9c + d9118f7 commit 49c6fd7

File tree

9 files changed

+211
-51
lines changed

9 files changed

+211
-51
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.thread.lock;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantReadWriteLock;
5+
6+
public class ReadWriteLockDemo {
7+
public static void main(String[] args) {
8+
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
9+
ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
10+
ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
11+
Task02 task02 = new Task02(readLock);
12+
Task02 task03 = new Task02(writeLock);
13+
for (int i = 0; i < 5; i++) {
14+
// new Thread(task02).start();
15+
new Thread(task03).start();
16+
}
17+
18+
}
19+
}
20+
21+
class Task02 implements Runnable {
22+
23+
Lock lock;
24+
25+
public Task02(Lock lock) {
26+
this.lock = lock;
27+
}
28+
29+
@Override
30+
public void run() {
31+
lock.lock();
32+
System.out.println(lock.getClass().getName() + " " + Thread.currentThread().getName());
33+
lock.unlock();
34+
}
35+
}
36+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.thread.lock;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
public class ReentrantLockDemo {
7+
public static void main(String[] args) {
8+
Task01 task01 = new Task01();
9+
new Thread(task01).start();
10+
new Thread(task01).start();
11+
}
12+
}
13+
14+
class Task01 implements Runnable {
15+
Lock lock = new ReentrantLock();
16+
17+
@Override
18+
public void run() {
19+
lock.lock();
20+
for (int i = 0; i < 10; i++) {
21+
System.out.println(Thread.currentThread().getName() + "运行 ------------" + i);
22+
}
23+
lock.unlock();
24+
}
25+
}

04spring/spring01/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
<groupId>org.springframework.boot</groupId>
2727
<artifactId>spring-boot-starter</artifactId>
2828
</dependency>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>4.12</version>
33+
</dependency>
34+
35+
2936
</dependencies>
3037

3138

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.github.yibing.spring.stream;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import lombok.ToString;
7+
8+
@Data
9+
@AllArgsConstructor
10+
@NoArgsConstructor
11+
@ToString
12+
public class Employee {
13+
private int id;
14+
private String name;
15+
private int age;
16+
private Double salary;
17+
18+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package org.github.yibing.spring.stream;
2+
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
9+
public class StreamDemo {
10+
List<Employee> emps = Arrays.asList(new Employee(101, "张三", 28, 9999d),
11+
new Employee(101, "李四", 49, 666d),
12+
new Employee(102, "王五", 38, 333d),
13+
new Employee(103, "赵六", 12, 7777d),
14+
new Employee(104, "田七", 6, 222d)
15+
);
16+
17+
public static void main(String[] args) {
18+
/**
19+
* stream 的创建方式
20+
*/
21+
22+
// 集合创建
23+
ArrayList<String> list = new ArrayList<>();
24+
Stream<String> stream = list.stream();
25+
// 数组创建
26+
Integer[] integers = new Integer[10];
27+
Stream<Integer> stream1 = Arrays.stream(integers);
28+
// Stream静态方法传概念
29+
Stream<Integer> stream2 = Stream.of(1, 2, 3, 4, 5, 6);
30+
// 创建无线流
31+
Stream<Integer> stream3 = Stream.iterate(0, (x) -> x + 1);
32+
33+
34+
}
35+
36+
// 中间操作-过滤
37+
@Test
38+
public void test1() {
39+
emps.stream()
40+
.filter((e) -> e.getAge() > 20)
41+
.limit(2) // limit(long n) :截取前面 n 条 , skip(long n):跳过前面 n 条
42+
.forEach(System.out::println);
43+
}
44+
45+
// 中间操作-映射
46+
@Test
47+
public void test2() {
48+
List<String> strList = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
49+
Stream<String> stream = strList.stream().map(String::toUpperCase);
50+
stream.forEach(System.out::println);
51+
52+
// Stream<Stream<Character>> stream1 = strList.stream().map(StreamDemo::filterCharacter);
53+
// stream1.forEach((sm) -> {
54+
// sm.forEach(System.out::print);
55+
// });
56+
Stream<Character> stream2 = strList.stream().flatMap(StreamDemo::filterCharacter);
57+
stream2.forEach(System.out::print);
58+
59+
}
60+
61+
// 中间操作-排序
62+
@Test
63+
public void test3() {
64+
emps.stream()
65+
.map(Employee::getName)
66+
.sorted() // 排序
67+
.forEach(System.out::println);
68+
69+
emps.stream()
70+
.sorted((x, y) -> {
71+
if (x.getAge() == y.getAge()) {
72+
return x.getName().compareTo(y.getName());
73+
} else {
74+
return Integer.compare(x.getAge(), y.getAge());
75+
}
76+
}) // 排序
77+
.forEach(System.out::println);
78+
}
79+
80+
// 查找与匹配
81+
@Test
82+
public void test4() {
83+
// 查看每个名字中是否都包含"五" ,这里返回false
84+
boolean b = emps.stream().allMatch((e) -> e.getName().contains("五"));
85+
System.out.println(b);
86+
// 返回第一个
87+
Optional<Employee> first = emps.stream().findFirst();
88+
System.out.println(first.get());
89+
}
90+
91+
// 归约
92+
@Test
93+
public void test5() {
94+
// 从0开始加 ,加到10
95+
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
96+
Integer sum = list.stream().reduce(0, (x, y) -> x + y);
97+
System.out.println(sum);
98+
99+
// 求所有员工的薪水总和
100+
Optional<Double> op = emps.stream().map(Employee::getSalary).reduce(Double::sum);
101+
System.out.println(op.get());
102+
}
103+
104+
//收集 : 将流转换成其他形式,collect(Collectors.())
105+
@Test
106+
public void test6() {
107+
List<String> list = emps.stream().map(Employee::getName).collect(Collectors.toList());
108+
list.forEach(System.out::println);
109+
Set<String> set = emps.stream().map(Employee::getName).collect(Collectors.toSet());
110+
set.forEach(System.out::println);
111+
112+
// 薪水求和
113+
Double sum = emps.stream().collect(Collectors.summingDouble(Employee::getSalary));
114+
//求平均薪水
115+
Double avg = emps.stream().collect(Collectors.averagingDouble(Employee::getSalary));
116+
}
117+
118+
public static Stream<Character> filterCharacter(String str) {
119+
ArrayList<Character> list = new ArrayList<>();
120+
for (Character ch : str.toCharArray()) {
121+
list.add(ch);
122+
}
123+
return list.stream();
124+
}
125+
}
-16 Bytes
Binary file not shown.

04spring/spring01/target/classes/application.properties

Whitespace-only changes.

04spring/spring01/target/classes/applicationContext.xml

Lines changed: 0 additions & 21 deletions
This file was deleted.

04spring/spring01/target/classes/instantiate-type.xml

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)