Skip to content

Commit 38c157b

Browse files
committed
refactor: reduce SuppressWarnings scope; use Thread#sleep
1 parent 892ff88 commit 38c157b

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

src/main/java/com/oldratlee/fucking/concurrency/HashMapHangDemo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
* @author Jerry Lee (oldratlee at gmail dot com)
99
* @see <a href="http://coolshell.cn/articles/9606.html">Infinite loop of Java HashMap</a> by <a href="http://github.com/haoel">@haoel</a>
1010
*/
11-
@SuppressWarnings("InfiniteLoopStatement")
1211
public class HashMapHangDemo {
1312
final Map<Integer, Object> holder = new HashMap<>();
1413

14+
@SuppressWarnings("InfiniteLoopStatement")
1515
public static void main(String[] args) {
1616
HashMapHangDemo demo = new HashMapHangDemo();
1717
for (int i = 0; i < 100; i++) {
@@ -43,6 +43,7 @@ private class ConcurrencyTask implements Runnable {
4343
private final Random random = new Random();
4444

4545
@Override
46+
@SuppressWarnings("InfiniteLoopStatement")
4647
public void run() {
4748
System.out.println("Add loop started in task!");
4849
while (true) {

src/main/java/com/oldratlee/fucking/concurrency/InvalidLongDemo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/**
44
* @author Jerry Lee(oldratlee at gmail dot com)
55
*/
6-
@SuppressWarnings("InfiniteLoopStatement")
76
public class InvalidLongDemo {
87
long count = 0;
98

9+
@SuppressWarnings("InfiniteLoopStatement")
1010
public static void main(String[] args) {
1111
// LoadMaker.makeLoad();
1212

@@ -28,6 +28,7 @@ ConcurrencyCheckTask getConcurrencyCheckTask() {
2828

2929
private class ConcurrencyCheckTask implements Runnable {
3030
@Override
31+
@SuppressWarnings("InfiniteLoopStatement")
3132
public void run() {
3233
int c = 0;
3334
for (int i = 0; ; i++) {

src/main/java/com/oldratlee/fucking/concurrency/NoPublishDemo.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.oldratlee.fucking.concurrency;
22

3-
import com.oldratlee.fucking.concurrency.util.Utils;
4-
53
/**
64
* <a href="https://hllvm-group.iteye.com/group/topic/34932">请问R大 有没有什么工具可以查看正在运行的类的c/汇编代码</a>提到了<b>代码提升</b>的问题。
75
*
@@ -10,15 +8,15 @@
108
public class NoPublishDemo {
119
boolean stop = false;
1210

13-
public static void main(String[] args) {
11+
public static void main(String[] args) throws Exception {
1412
// LoadMaker.makeLoad();
1513

1614
NoPublishDemo demo = new NoPublishDemo();
1715

1816
Thread thread = new Thread(demo.getConcurrencyCheckTask());
1917
thread.start();
2018

21-
Utils.sleep(1000);
19+
Thread.sleep(1000);
2220
System.out.println("Set stop to true in main!");
2321
demo.stop = true;
2422
System.out.println("Exit main.");
@@ -28,15 +26,15 @@ ConcurrencyCheckTask getConcurrencyCheckTask() {
2826
return new ConcurrencyCheckTask();
2927
}
3028

31-
@SuppressWarnings({"StatementWithEmptyBody", "WhileLoopSpinsOnField"})
3229
private class ConcurrencyCheckTask implements Runnable {
3330
@Override
31+
@SuppressWarnings({"WhileLoopSpinsOnField", "StatementWithEmptyBody"})
3432
public void run() {
3533
System.out.println("ConcurrencyCheckTask started!");
3634
// If the value of stop is visible in the main thread, the loop will exit.
3735
// On my dev machine, the loop almost never exits!
3836
// Simple and safe solution:
39-
// add volatile to the running field.
37+
// add volatile to the `stop` field.
4038
while (!stop) {
4139
}
4240
System.out.println("ConcurrencyCheckTask stopped!");

src/main/java/com/oldratlee/fucking/concurrency/util/LoadMaker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static void stopLoad() {
3333
isLoadMade = false;
3434
}
3535

36-
public static void sleep(long millis) {
36+
private static void sleep(long millis) {
3737
try {
3838
Thread.sleep(millis);
3939
} catch (InterruptedException ignored) {

0 commit comments

Comments
 (0)