Skip to content

Commit 1424730

Browse files
committed
数据结构
1 parent a506738 commit 1424730

29 files changed

+2214
-1
lines changed

src/main/java/code/collection/MinHeap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* 〈最小堆〉<p>
77
* 固定容量,解决TopK问题,满足公平性,即当值相等时,保留先放入的值
8-
*
8+
* 复杂度:log(k)
99
* @author zixiao
1010
* @date 2019/2/21
1111
*/
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package code.collection.bitmap;
2+
3+
import code.collection.hashtable.HashMap;
4+
5+
import java.util.BitSet;
6+
7+
/**
8+
* 〈位图〉<p>
9+
* 〈功能详细描述〉
10+
*
11+
* @author zixiao
12+
* @date 2019/12/16
13+
*/
14+
public class BitMap {
15+
16+
private BitSet bitSet;
17+
18+
private int capacity;
19+
20+
private HashMap<Integer, Integer> conflictCounter;
21+
22+
public BitMap(int capacity) {
23+
this.capacity = capacity;
24+
bitSet = new BitSet(capacity);
25+
conflictCounter = new HashMap<>(capacity / 16);
26+
}
27+
28+
public void set(int n){
29+
if (get(n)) {
30+
Integer count = conflictCounter.get(n);
31+
if (count == null) {
32+
conflictCounter.put(n, 2);
33+
} else {
34+
conflictCounter.put(n, ++count);
35+
}
36+
} else {
37+
bitSet.set(n);
38+
}
39+
}
40+
41+
public boolean get(int n){
42+
return bitSet.get(n);
43+
}
44+
45+
public static void main(String[] args) {
46+
int[] a = {10, 3, 4, 30, 9, 29, 60, 13, 40, 43, 3};
47+
int capacity = a.length * 10;
48+
BitMap bitMap = new BitMap(capacity);
49+
for (int i : a) {
50+
bitMap.set(i);
51+
}
52+
for (int i = 0; i < capacity; i++) {
53+
if(bitMap.get(i)){
54+
Integer count = bitMap.conflictCounter.get(i);
55+
count = count == null ? 1 : count;
56+
for (Integer j = 0; j < count; j++) {
57+
System.out.print(i);
58+
System.out.print("\t");
59+
}
60+
}
61+
}
62+
}
63+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package code.collection.disruptor;
2+
3+
/**
4+
* 〈一句话功能简述〉<p>
5+
* 〈功能详细描述〉
6+
*
7+
* @author zixiao
8+
* @date 2019/9/25
9+
*/
10+
public class LogEvent {
11+
12+
private String value;
13+
14+
public String getValue() {
15+
return value;
16+
}
17+
18+
public void setValue(String value) {
19+
this.value = value;
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package code.collection.disruptor;
2+
3+
import com.lmax.disruptor.EventFactory;
4+
5+
/**
6+
* 〈一句话功能简述〉<p>
7+
* 〈功能详细描述〉
8+
*
9+
* @author zixiao
10+
* @date 2019/9/25
11+
*/
12+
public class LogEventFactory implements EventFactory<LogEvent> {
13+
14+
@Override
15+
public LogEvent newInstance() {
16+
return new LogEvent();
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package code.collection.disruptor;
2+
3+
import com.lmax.disruptor.RingBuffer;
4+
5+
/**
6+
* 〈一句话功能简述〉<p>
7+
* 〈功能详细描述〉
8+
*
9+
* @author zixiao
10+
* @date 2019/9/25
11+
*/
12+
public class LogEventProducer {
13+
14+
private final RingBuffer<LogEvent> ringBuffer;
15+
16+
public LogEventProducer(RingBuffer<LogEvent> ringBuffer) {
17+
this.ringBuffer = ringBuffer;
18+
}
19+
20+
public void produce(String s) {
21+
long seq = ringBuffer.next();
22+
try {
23+
LogEvent event = ringBuffer.get(seq);
24+
event.setValue(s);
25+
} finally {
26+
ringBuffer.publish(seq);
27+
}
28+
}
29+
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package code.collection.disruptor;
2+
3+
import com.lmax.disruptor.EventTranslatorOneArg;
4+
import com.lmax.disruptor.RingBuffer;
5+
6+
/**
7+
* 〈一句话功能简述〉<p>
8+
* 〈功能详细描述〉
9+
*
10+
* @author zixiao
11+
* @date 2019/9/25
12+
*/
13+
public class LogEventProducerWithTranslator {
14+
15+
private static final EventTranslatorOneArg<LogEvent, String> TRANSLATOR =
16+
(event, sequence, s) -> event.setValue(s);
17+
18+
private final RingBuffer<LogEvent> ringBuffer;
19+
20+
public LogEventProducerWithTranslator(RingBuffer<LogEvent> ringBuffer) {
21+
this.ringBuffer = ringBuffer;
22+
}
23+
24+
public void produce(String s) {
25+
ringBuffer.publishEvent(TRANSLATOR, s);
26+
}
27+
28+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package code.collection.disruptor;
2+
3+
import com.lmax.disruptor.EventHandler;
4+
import com.lmax.disruptor.RingBuffer;
5+
import com.lmax.disruptor.dsl.Disruptor;
6+
import com.lmax.disruptor.util.DaemonThreadFactory;
7+
import org.apache.commons.lang3.time.StopWatch;
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import java.util.concurrent.ArrayBlockingQueue;
13+
import java.util.concurrent.BlockingQueue;
14+
import java.util.concurrent.atomic.AtomicInteger;
15+
16+
/**
17+
* 〈一句话功能简述〉<p>
18+
* 〈功能详细描述〉
19+
*
20+
* @author zixiao
21+
* @date 2019/9/25
22+
*/
23+
public class LogEventTest {
24+
25+
private static int THREADS = 64;
26+
27+
private static int PER_NUM = 1024*1024;
28+
29+
private static int TOTAL_NUM = THREADS*PER_NUM;
30+
31+
private static int CAPACITY = 4096;
32+
33+
private Disruptor<LogEvent> disruptor;
34+
35+
private LogEventProducer producer;
36+
37+
private AtomicInteger count = new AtomicInteger(0);
38+
39+
@Before
40+
public void before(){
41+
// The factory for the event
42+
LogEventFactory factory = new LogEventFactory();
43+
44+
// Specify the size of the ring buffer, must be power of 2.
45+
int bufferSize = CAPACITY;
46+
47+
// Construct the Disruptor
48+
disruptor = new Disruptor<>(factory, bufferSize, DaemonThreadFactory.INSTANCE);
49+
50+
// Connect the handler
51+
disruptor.handleEventsWith(new LogEventHandler());
52+
53+
// Start the Disruptor, starts all threads running
54+
disruptor.start();
55+
56+
// Get the ring buffer from the Disruptor to be used for publishing.
57+
RingBuffer<LogEvent> ringBuffer = disruptor.getRingBuffer();
58+
59+
this.producer = new LogEventProducer(ringBuffer);
60+
}
61+
62+
@Test
63+
public void testDisruptor() {
64+
StopWatch stopWatch = new StopWatch();
65+
stopWatch.start();
66+
for (int t = 0; t < THREADS; t++) {
67+
Thread thread = new Thread(()->{
68+
for(int i=0; i<PER_NUM; i++){
69+
producer.produce("Test");
70+
}
71+
});
72+
thread.start();
73+
}
74+
while (count.get() != TOTAL_NUM){}
75+
stopWatch.stop();
76+
77+
System.out.println("Disruptor Count "+ count.get() +", cost "+ stopWatch.getTime() + "ms");
78+
}
79+
80+
@Test
81+
public void testBlockingQueue(){
82+
BlockingQueue<LogEvent> queue = new ArrayBlockingQueue<LogEvent>(CAPACITY);
83+
Thread consumer = new Thread(()->{
84+
while (true){
85+
try {
86+
LogEvent event = queue.take();
87+
count.addAndGet(1);
88+
} catch (InterruptedException e) {
89+
e.printStackTrace();
90+
}
91+
}
92+
});
93+
consumer.start();
94+
95+
StopWatch stopWatch = new StopWatch();
96+
stopWatch.start();
97+
for (int t = 0; t < THREADS; t++) {
98+
Thread thread = new Thread(()->{
99+
for(int i=0; i<PER_NUM; i++){
100+
LogEvent event = new LogEvent();
101+
event.setValue("Test");
102+
try {
103+
queue.put(event);
104+
} catch (InterruptedException e) {
105+
e.printStackTrace();
106+
}
107+
}
108+
});
109+
thread.start();
110+
}
111+
while (count.get() != TOTAL_NUM){}
112+
stopWatch.stop();
113+
114+
System.out.println("BlockingQueue Count "+ count.get() +", cost "+ stopWatch.getTime() + "ms");
115+
}
116+
117+
@After
118+
public void after(){
119+
disruptor.shutdown();
120+
}
121+
122+
class LogEventHandler implements EventHandler<LogEvent> {
123+
124+
@Override
125+
public void onEvent(LogEvent event, long sequence, boolean endOfBatch) throws Exception {
126+
//System.out.println("Event:" + event.getValue() + ", seq:" + sequence);
127+
count.addAndGet(1);
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)