Skip to content

Commit dc0ed11

Browse files
committed
feat:中间件、springcloud等案例
1 parent ceb171b commit dc0ed11

123 files changed

Lines changed: 3786 additions & 109 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

java-example/java-algorithm-example/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
xmlns="http://maven.apache.org/POM/4.0.0"
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
55
<modelVersion>4.0.0</modelVersion>
6+
<build>
7+
<plugins>
8+
<plugin>
9+
<groupId>org.apache.maven.plugins</groupId>
10+
<artifactId>maven-compiler-plugin</artifactId>
11+
<configuration>
12+
<source>1.8</source>
13+
<target>1.8</target>
14+
</configuration>
15+
</plugin>
16+
</plugins>
17+
</build>
618
<parent>
719
<groupId>com.housaire</groupId>
820
<artifactId>java-example</artifactId>

java-example/java-algorithm-example/src/main/java/com/housaire/App.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.sql.Timestamp;
44
import java.text.SimpleDateFormat;
55
import java.util.Date;
6+
import java.util.TreeMap;
67

78
/**
89
* Hello world!
@@ -48,7 +49,7 @@ public void sayHello()
4849

4950
public static void main(String[] args) throws CloneNotSupportedException
5051
{
51-
A a = new A(10);
52+
/*A a = new A(10);
5253
a.b = 5;
5354
a. c = -19;
5455
A b = a.clone();
@@ -70,7 +71,12 @@ public static void main(String[] args) throws CloneNotSupportedException
7071
System.out.println(a);
7172
7273
System.out.println(a.getClass().getClassLoader());
73-
System.out.println(String.class.getClassLoader());
74+
System.out.println(String.class.getClassLoader());*/
75+
76+
77+
Integer i1 = new Integer(1);
78+
Integer i2 = new Integer(2);
79+
System.out.println("Integer == " + (i1 == i2));
7480

7581
}
7682

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.housaire;
2+
3+
import java.util.concurrent.*;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
/**
7+
* Created by kay on 2019/12/7.
8+
*/
9+
public class App1 {
10+
11+
private static final int COUNT_BITS = Integer.SIZE - 3;
12+
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
13+
private static final int RUNNING = -1 << COUNT_BITS;
14+
private static final int SHUTDOWN = 0 << COUNT_BITS;
15+
private static final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
16+
private static int runStateOf(int c) { return c & ~CAPACITY; }
17+
private static int workerCountOf(int c) { return c & CAPACITY; }
18+
private static int ctlOf(int rs, int wc) { return rs | wc; }
19+
20+
private static boolean isRunning(int c) {
21+
return c < SHUTDOWN;
22+
}
23+
24+
public static void main(String[] args) throws Exception {
25+
System.out.println("runStateOf: " + runStateOf(ctl.get()));
26+
System.out.println("SHUTDOWN: " + SHUTDOWN);
27+
System.out.println("workerCountOf: " + workerCountOf(ctl.get()));
28+
System.out.println("CAPACITY: " + CAPACITY);
29+
System.out.println("isRunning: " + isRunning(ctl.get()));
30+
31+
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
32+
cachedThreadPool.execute(new Runnable() {
33+
@Override
34+
public void run() {
35+
System.out.println("111");
36+
}
37+
});
38+
39+
final SynchronousQueue queue = new SynchronousQueue();
40+
41+
CountDownLatch cdl = new CountDownLatch(1);
42+
43+
cachedThreadPool.execute(new Runnable() {
44+
@Override
45+
public void run() {
46+
try {
47+
cdl.countDown();
48+
System.out.println("Queue->take: " + queue.take());
49+
System.out.println("Queue->take: " + queue.take());
50+
} catch (InterruptedException e) {
51+
e.printStackTrace();
52+
}
53+
}
54+
});
55+
56+
cdl.await();
57+
System.out.println("Queue->offer: " + queue.offer("abc"));
58+
queue.put("321");
59+
System.out.println("finished");
60+
61+
cachedThreadPool.shutdown();
62+
}
63+
64+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.housaire;
2+
3+
/**
4+
* Created by kay on 2019/12/2.
5+
*/
6+
public class InnerClassDemo {
7+
8+
public void sayHello() {
9+
System.out.println("Out say hello");
10+
}
11+
12+
class StaticInnerClass {
13+
14+
public void studing() {
15+
sayHello();
16+
}
17+
18+
}
19+
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.housaire;
2+
3+
/**
4+
* Created by kay on 2019/12/12.
5+
*/
6+
public class IntReverse {
7+
8+
public static int reverse(int num) {
9+
int rev = 0;
10+
while (num != 0) {
11+
rev = rev * 10 + num % 10;
12+
num /= 10;
13+
}
14+
return rev;
15+
}
16+
17+
public static void main(String[] args) {
18+
System.out.println(reverse(-199));
19+
}
20+
21+
}

java-example/java-algorithm-example/src/main/java/com/housaire/RandomRedPacket.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ public static void main(String[] args)
2525
float amount = 10;
2626
int num = 900;
2727
double total = 0;
28+
int a = 0, b = 0, c = 0;
2829
for (int i = num; i > 0; i--)
2930
{
3031
double money = RandomRedPacket.getRandomMoney(i, amount);
3132
total += money;
3233
amount = (float) (amount - money);
3334
System.out.println("第" + (num - i) + "个红包:" + money);
35+
if (money == 0.01) a++;
36+
else if(money == 0.02) b++;
37+
else c++;
3438
}
39+
System.out.println("得到 0.01 元红包的人数为:" + a);
40+
System.out.println("得到 0.02 元红包的人数为:" + b);
41+
System.out.println("得到 0.03 元红包的人数为:" + c);
3542
System.out.println(total);
3643
}
3744

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.housaire;
2+
3+
/**
4+
* Created by kay on 2019/12/2.
5+
*/
6+
public class SafeListener {
7+
private final EventListener listener;
8+
9+
private SafeListener() {
10+
listener = new EventListener() {
11+
@Override
12+
public void onEvent(Event e) {
13+
doSomething(e);
14+
}
15+
};
16+
}
17+
18+
public static SafeListener newInstance(EventSource source) {
19+
SafeListener safe = new SafeListener();
20+
source.registerListener(safe.listener);
21+
return safe;
22+
}
23+
24+
void doSomething(Event e) {
25+
System.out.println("hi event " + e);
26+
}
27+
28+
interface EventSource {
29+
void registerListener(EventListener e);
30+
}
31+
32+
interface EventListener {
33+
void onEvent(Event e);
34+
}
35+
36+
interface Event {
37+
}
38+
39+
public static void main(String[] args) {
40+
SafeListener listener = SafeListener.newInstance(new EventSource() {
41+
@Override
42+
public void registerListener(EventListener e) {
43+
e.onEvent(new Event() {
44+
});
45+
}
46+
});
47+
}
48+
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.housaire.cache;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Created by kay on 2019/11/28.
8+
*/
9+
public class LRUCache extends LinkedHashMap {
10+
11+
private int maxSize;
12+
13+
public LRUCache(int maxSize)
14+
{
15+
super(0, 0.75f, true);
16+
this.maxSize = maxSize;
17+
}
18+
19+
@Override
20+
protected boolean removeEldestEntry(Map.Entry eldest) {
21+
return this.size() > maxSize;
22+
}
23+
24+
25+
public static void main(String[] args) {
26+
LRUCache lruCache = new LRUCache(10);
27+
for (int i = 0; i < 10; i++)
28+
{
29+
lruCache.put(i, i);
30+
}
31+
System.out.println(lruCache);
32+
33+
lruCache.get(0);
34+
lruCache.put(11, 11);
35+
System.out.println(lruCache);
36+
}
37+
38+
39+
}

java-example/java-algorithm-example/src/main/java/com/housaire/list/linked/LinkedListDivideEqually.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
/**
44
* 链表三等份
5+
* 设三个链表: 第一个每循环一次走一步
6+
* 第二个每循环一次走两步
7+
* 第三个每循环一次走三步
58
*/
69
public class LinkedListDivideEqually
710
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.housaire;
2+
3+
/**
4+
* Created by kay on 2019/9/16.
5+
*/
6+
public class ClassLoaderDemo {
7+
8+
public static void main(String[] args) {
9+
System.out.println(A.class);
10+
}
11+
}
12+
13+
class A {
14+
15+
static {
16+
System.err.println("class A");
17+
}
18+
19+
}

0 commit comments

Comments
 (0)