Skip to content

Commit 150d2f6

Browse files
committed
HashMap内存溢出
1 parent bb8519e commit 150d2f6

File tree

4 files changed

+150
-3
lines changed

4 files changed

+150
-3
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Senior Java engineer interview exams in 2019
88

99
### 面试题来自本人的程序员朋友面试经历,经本人和开源爱好者汇总
1010

11+
* 互联网大厂常考编程题
12+
* [Hot!大厂常考编程题,点击进入](readme/pop-exam.md)
13+
1114
* 拍拍贷
1215
* [使用HashMap的时候如何避免内存泄漏](readme/hashmap-memory-leak.md)
1316

@@ -214,8 +217,9 @@ String s = " z111,c888,n222,,,g000, t333,a999,c888 ,p000 ,z111 ";
214217
* valotile关键字有什么作用
215218

216219
* duboo或者spring cloud微服务调用超时应该怎么处理
217-
218-
220+
221+
* sql注入与防止sql注入的方法
222+
219223
<hr/>
220224

221225
* 京东金融
@@ -269,7 +273,11 @@ String s = " z111,c888,n222,,,g000, t333,a999,c888 ,p000 ,z111 ";
269273

270274
*2.如何自己去实现一个WEB框架,怎么实现类似于SpringMVC里的@RequestMapping,用什么匹配算法速度最快
271275

272-
276+
* 蚂蚁金服
277+
* 什么是对称加密和非对称加密
278+
279+
280+
273281
## 更多Java面试题
274282

275283
部分题目的答案和实例代码在陆续整理中,你们也可以贡献你们的答案。

examset/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.lsm</groupId>
8+
<artifactId>examset</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.lsm.roundrobinprint;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.locks.Condition;
8+
import java.util.concurrent.locks.Lock;
9+
import java.util.concurrent.locks.ReentrantLock;
10+
11+
/**
12+
* ReentrantLock-Condition实现三个线程交替打印1-75
13+
* 结果如下:
14+
* <p>
15+
* Thread[pool-1-thread-1,5,main]1
16+
* Thread[pool-1-thread-2,5,main]2
17+
* Thread[pool-1-thread-3,5,main]3
18+
* Thread[pool-1-thread-1,5,main]4
19+
* Thread[pool-1-thread-2,5,main]5
20+
* Thread[pool-1-thread-3,5,main]6
21+
* Thread[pool-1-thread-1,5,main]7
22+
* Thread[pool-1-thread-2,5,main]8
23+
* Thread[pool-1-thread-3,5,main]9
24+
* ...
25+
* Thread[pool-1-thread-1,5,main]73
26+
* Thread[pool-1-thread-2,5,main]74
27+
* Thread[pool-1-thread-3,5,main]75
28+
* <p>
29+
* Condition的含义:
30+
* 通过调用condition.await()方法,可以让当前线程在该条件下等待;
31+
* 当通过调用condition.signal()方法,又可以唤醒该条件下的等待的线程;
32+
*/
33+
public class RobinPrintReentrantLock {
34+
private static final int TASK_NUM = 3;
35+
private static int num = 0;
36+
private static int flag = 0;
37+
private static Lock lock = new ReentrantLock();
38+
private static List<Condition> list = new ArrayList<Condition>();
39+
private static ExecutorService exec = Executors.newCachedThreadPool();
40+
41+
static {
42+
for (int i = 0; i < TASK_NUM; i++) {
43+
list.add(lock.newCondition());
44+
}
45+
}
46+
47+
private static void crit() {
48+
if (num >= 75) {
49+
System.exit(1);
50+
}
51+
}
52+
53+
private static void print() {
54+
crit();
55+
System.out.print(Thread.currentThread());
56+
// for (int i = 0; i < 5; i++) {
57+
// System.out.format("%-2d ", ++num);
58+
// }
59+
60+
System.out.format("%-2d ", ++num);
61+
System.out.println();
62+
}
63+
64+
private static void work(int i) {
65+
while (!Thread.interrupted()) {
66+
lock.lock();
67+
try {
68+
if (flag == i) {
69+
print();
70+
flag = (i + 1) % list.size();
71+
list.get(flag).signal();
72+
} else {
73+
try {
74+
list.get(i % list.size()).await();
75+
} catch (InterruptedException e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
} finally {
80+
lock.unlock();
81+
}
82+
}
83+
}
84+
85+
private static class Task implements Runnable {
86+
private final int i;
87+
88+
public Task(int i) {
89+
this.i = i;
90+
}
91+
92+
@Override
93+
public void run() {
94+
work(i);
95+
}
96+
97+
}
98+
99+
/**
100+
* @param args
101+
*/
102+
public static void main(String[] args) {
103+
for (int i = 0; i < list.size(); i++)
104+
exec.execute(new Task(i));
105+
}
106+
}

readme/pop-exam.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 互联网大厂常考编程题
2+
3+
1.eentrantLock-Condition实现三个线程交替打印1-75
4+
5+
比如结果
6+
```text
7+
* Thread[pool-1-thread-1,5,main]1
8+
* Thread[pool-1-thread-2,5,main]2
9+
* Thread[pool-1-thread-3,5,main]3
10+
* Thread[pool-1-thread-1,5,main]4
11+
* Thread[pool-1-thread-2,5,main]5
12+
* Thread[pool-1-thread-3,5,main]6
13+
* Thread[pool-1-thread-1,5,main]7
14+
* Thread[pool-1-thread-2,5,main]8
15+
* Thread[pool-1-thread-3,5,main]9
16+
* ...
17+
* Thread[pool-1-thread-1,5,main]73
18+
* Thread[pool-1-thread-2,5,main]74
19+
* Thread[pool-1-thread-3,5,main]75
20+
```
21+
代码见代码[RobinPrintReentrantLock.java](/examset/src/main/java/com/lsm/roundrobinprint/RobinPrintReentrantLock.java)

0 commit comments

Comments
 (0)