Skip to content

Commit c16215f

Browse files
author
fumj
committed
1.threadlocal 使用
2.thread chapter1 3.策略模式
1 parent 621e54b commit c16215f

19 files changed

Lines changed: 760 additions & 0 deletions

designpatten/pom.xml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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>example</artifactId>
7+
<groupId>com.fumj</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>designpatten</artifactId>
13+
14+
<dependencies>
15+
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
16+
<dependency>
17+
<groupId>org.junit.jupiter</groupId>
18+
<artifactId>junit-jupiter-api</artifactId>
19+
<version>5.3.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
23+
24+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
25+
<dependency>
26+
<groupId>org.springframework</groupId>
27+
<artifactId>spring-context</artifactId>
28+
<version>5.2.1.RELEASE</version>
29+
</dependency>
30+
31+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
32+
<dependency>
33+
<groupId>org.springframework</groupId>
34+
<artifactId>spring-core</artifactId>
35+
<version>5.2.1.RELEASE</version>
36+
</dependency>
37+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
38+
<dependency>
39+
<groupId>org.springframework</groupId>
40+
<artifactId>spring-beans</artifactId>
41+
<version>5.2.1.RELEASE</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.projectlombok</groupId>
46+
<artifactId>lombok</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>cn.hutool</groupId>
50+
<artifactId>hutool-all</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>log4j</groupId>
54+
<artifactId>log4j</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.slf4j</groupId>
58+
<artifactId>slf4j-log4j12</artifactId>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>org.slf4j</groupId>
63+
<artifactId>slf4j-api</artifactId>
64+
</dependency><dependency>
65+
<groupId>junit</groupId>
66+
<artifactId>junit</artifactId>
67+
</dependency>
68+
</dependencies>
69+
</project>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package rdis;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author fumj
8+
* @projectName example
9+
* @description: TODO
10+
* @date 2019/12/2517:43
11+
*/
12+
public class RedisLRU<k, v> {
13+
private int capacity;
14+
15+
private int count;
16+
17+
private Map<k, Node<k, v>> nodeMap;
18+
19+
private Node<k, v> head;
20+
21+
private Node<k, v> tail;
22+
23+
public RedisLRU(int capacity) {
24+
if (capacity < 1) {
25+
throw new IllegalArgumentException(String.valueOf(capacity));
26+
}
27+
this.capacity = capacity;
28+
this.nodeMap = new HashMap<>();
29+
Node headNode = new Node(null,null);
30+
Node tailNode = new Node(null,null);
31+
headNode.next = tailNode;
32+
tailNode.pre = headNode;
33+
this.head = headNode;
34+
this.tail = tailNode;
35+
}
36+
37+
public void put(k key,v value){
38+
Node<k,v> node = nodeMap.get(key);
39+
if(node == null) {
40+
if (count >= capacity) {
41+
removeNode();
42+
}
43+
node = new Node<>(key,value);
44+
addNode(node);
45+
}else {
46+
moveNodeToHead(node);
47+
}
48+
}
49+
50+
51+
public Node<k,v> get(k key){
52+
Node<k,v> node = nodeMap.get(key);
53+
if(node != null){
54+
moveNodeToHead(node);
55+
}
56+
return node;
57+
}
58+
59+
public void removeNode(){
60+
Node node = tail.pre;
61+
removeFromList(node);
62+
nodeMap.remove(node.key);
63+
count--;
64+
}
65+
66+
private void removeFromList(Node<k,v> node){
67+
Node pre = node.pre;
68+
Node next = node.next;
69+
70+
pre.next = next;
71+
next.pre = pre;
72+
node.next = null;
73+
node.pre = null;
74+
}
75+
76+
private void addNode(Node<k,v> node){
77+
addToHead(node);
78+
nodeMap.put(node.key,node);
79+
count++;
80+
}
81+
82+
public void moveNodeToHead(Node<k,v> node){
83+
removeFromList(node);
84+
addToHead(node);
85+
}
86+
87+
public void addToHead(Node<k,v> node){
88+
Node next = head.next;
89+
next.pre = node;
90+
node.next = next;
91+
node.pre = head;
92+
head.next = node;
93+
}
94+
95+
96+
class Node<k, v> {
97+
k key;
98+
v value;
99+
Node pre;
100+
Node next;
101+
102+
public Node(k key,v value){
103+
this.key = key;
104+
this.value = value;
105+
}
106+
107+
}
108+
}
109+
110+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package singleton;
2+
3+
/**
4+
* @author fumj
5+
* @projectName credit
6+
* @description: TODO
7+
* @date 2019/6/2016:27
8+
*/
9+
public enum Singleton {
10+
INSTANCE;
11+
12+
public void doSomething() {
13+
System.out.println("do =============");
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package singleton;
2+
3+
/**
4+
* @author fumj
5+
* @projectName credit
6+
* @description: TODO
7+
* @date 2019/6/2016:28
8+
*/
9+
public class SingletonTest {
10+
public static void main(String[] args) {
11+
Singleton.INSTANCE.doSomething();
12+
}
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package strategy;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
/**
6+
* @author fumj
7+
* @projectName example
8+
* @description: TODO
9+
* @date 2019/12/2517:22
10+
*/
11+
@Slf4j
12+
public class EmailSlover extends ServerDealSlover {
13+
@Override
14+
public void slover(Long serverId, Long userId) {
15+
log.info("email slover");
16+
}
17+
18+
@Override
19+
public String[] supports() {
20+
return new String[]{"EMAIL"};
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package strategy;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
/**
6+
* @author fumj
7+
* @projectName example
8+
* @description: TODO
9+
* @date 2019/12/2517:25
10+
*/
11+
@Slf4j
12+
public class MessageSlover extends ServerDealSlover {
13+
@Override
14+
public void slover(Long serverId, Long userId) {
15+
log.info("message slover");
16+
}
17+
18+
@Override
19+
public String[] supports() {
20+
return new String[]{"MESSAGE"};
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package strategy;
2+
3+
/**
4+
* @author fumj
5+
* @projectName example
6+
* @description: TODO
7+
* @date 2019/12/2517:23
8+
*/
9+
public abstract class ServerDealSlover {
10+
public abstract void slover(Long serverId,Long userId);
11+
public abstract String[] supports();
12+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package strategy;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.ApplicationContextAware;
6+
7+
import javax.annotation.PostConstruct;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
/**
12+
* @author fumj
13+
* @projectName example
14+
* @description: TODO
15+
* @date 2019/12/2517:27
16+
*/
17+
public class ServerDealSloverChooser implements ApplicationContextAware {
18+
private ApplicationContext applicationContext;
19+
@Override
20+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
21+
this.applicationContext = applicationContext;
22+
}
23+
24+
private Map<String,ServerDealSlover> sloverMap = new HashMap<>();
25+
26+
@PostConstruct
27+
private void register(){
28+
Map<String, ServerDealSlover> beansOfType = applicationContext.getBeansOfType(ServerDealSlover.class);
29+
sloverMap.putAll(beansOfType);
30+
}
31+
32+
public ServerDealSlover chooser(String type){
33+
return sloverMap.get(type);
34+
}
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package strategy;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.BeansException;
5+
import org.springframework.context.ApplicationContext;
6+
import org.springframework.context.ApplicationContextAware;
7+
8+
import javax.annotation.PostConstruct;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
/**
13+
* @author fumj
14+
* @projectName example
15+
* @description: TODO
16+
* @date 2019/12/2517:27
17+
*/
18+
@Slf4j
19+
public class ServerDealSloverChooserTest {
20+
public static void main(String[] args) {
21+
ServerDealSloverChooser chooser = new ServerDealSloverChooser();
22+
ServerDealSlover slover = chooser.chooser("MESSAGE");
23+
slover.slover(1L,1L);
24+
}
25+
}

0 commit comments

Comments
 (0)