Skip to content

Commit b7c4803

Browse files
committed
adding dashboard demo controller
1 parent a532927 commit b7c4803

3 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.exampledriven.springboot;
2+
3+
import org.exampledriven.springboot.deadlock.Runnable1;
4+
import org.exampledriven.springboot.deadlock.Runnable2;
5+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* This is an example to show how the remote shell dashboard feature is useful
11+
*/
12+
@RestController
13+
@RequestMapping("/dashboard")
14+
public class DashboardDemoController {
15+
16+
protected static final int ONE_HOUR = 1000 * 60 * 60;
17+
18+
@RequestMapping("/sleep")
19+
public String makeMemoryLeak() {
20+
new SimpleAsyncTaskExecutor().execute(new Runnable() {
21+
22+
@Override
23+
public void run() {
24+
try {
25+
Thread.sleep(ONE_HOUR);
26+
} catch (InterruptedException e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
31+
});
32+
33+
return "thread is sleeping";
34+
}
35+
36+
@RequestMapping("/deadlock")
37+
public String makeDeadlock() {
38+
39+
Object lock1 = new Object();
40+
Object lock2 = new Object();
41+
42+
SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
43+
simpleAsyncTaskExecutor.setDaemon(true);
44+
45+
simpleAsyncTaskExecutor.execute(new Runnable1(lock1, lock2));
46+
simpleAsyncTaskExecutor.execute(new Runnable2(lock1, lock2));
47+
48+
return "Two threads are blocked now!";
49+
50+
}
51+
52+
53+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.exampledriven.springboot.deadlock;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class Runnable1 implements Runnable {
7+
8+
Logger logger = LoggerFactory.getLogger(Runnable1.class);
9+
10+
private Object lock2;
11+
private Object lock1;
12+
13+
public Runnable1(Object lock2, Object lock1) {
14+
this.lock2 = lock2;
15+
this.lock1 = lock1;
16+
}
17+
18+
public void run() {
19+
20+
synchronized (lock1) {
21+
logger.info("Holding lock 1...");
22+
23+
try {
24+
Thread.sleep(10);
25+
} catch (InterruptedException e) {
26+
}
27+
28+
logger.info("Waiting for lock 2...");
29+
30+
synchronized (lock2) {
31+
logger.info("Holding lock 1 & 2...");
32+
}
33+
}
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.exampledriven.springboot.deadlock;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class Runnable2 implements Runnable {
7+
8+
Logger logger = LoggerFactory.getLogger(Runnable2.class);
9+
10+
private Object lock2;
11+
private Object lock1;
12+
13+
public Runnable2(Object lock2, Object lock1) {
14+
this.lock2 = lock2;
15+
this.lock1 = lock1;
16+
}
17+
18+
public void run() {
19+
20+
synchronized (lock2) {
21+
logger.info("Holding lock 2...");
22+
23+
try {
24+
Thread.sleep(10);
25+
} catch (InterruptedException e) {
26+
}
27+
28+
logger.info("Waiting for lock 1...");
29+
30+
synchronized (lock1) {
31+
logger.info("Holding lock 1 & 2...");
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)