Skip to content

Commit 3be0e85

Browse files
committed
thread dumo example
1 parent 23cd4e4 commit 3be0e85

File tree

6 files changed

+275
-0
lines changed

6 files changed

+275
-0
lines changed

core-java/threaddump/pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>io.pratik.server</groupId>
5+
<artifactId>ServerApp</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>ServerApp</name>
9+
<url>http://maven.apache.org</url>
10+
<properties>
11+
<maven.compiler.source>11</maven.compiler.source>
12+
<maven.compiler.target>11</maven.compiler.target>
13+
</properties>
14+
<dependencies>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
<version>3.8.1</version>
19+
<scope>test</scope>
20+
</dependency>
21+
</dependencies>
22+
<build>
23+
<plugins>
24+
<plugin>
25+
<artifactId>maven-compiler-plugin</artifactId>
26+
<version>3.8.1</version>
27+
<configuration>
28+
<source>11</source>
29+
<target>11</target>
30+
</configuration>
31+
</plugin>
32+
<plugin>
33+
<!-- Build an executable JAR -->
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-jar-plugin</artifactId>
36+
<version>2.4</version>
37+
<configuration>
38+
<archive>
39+
<manifest>
40+
<mainClass>io.pratik.server.App</mainClass>
41+
</manifest>
42+
</archive>
43+
</configuration>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.pratik.server;
2+
3+
import java.net.ServerSocket;
4+
import java.util.logging.Logger;
5+
6+
/**
7+
* @author pratikdas
8+
*
9+
*/
10+
public class App {
11+
private static final Logger logger = Logger.getLogger(App.class.getName());
12+
13+
public static void main(String[] args) throws Exception {
14+
ServerSocket ssock = new ServerSocket(8080);
15+
logger.info("Server Started. Listening on port 8080");
16+
17+
while (true) {
18+
new RequestProcessor(ssock).handleClientRequest();;
19+
}
20+
}
21+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
*
3+
*/
4+
package io.pratik.server;
5+
6+
import java.io.BufferedReader;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
import java.io.OutputStream;
10+
import java.net.ServerSocket;
11+
import java.net.Socket;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.logging.Logger;
15+
16+
/**
17+
* @author pratikdas
18+
*
19+
*/
20+
public class RequestProcessor {
21+
private static final Logger logger = Logger.getLogger(RequestProcessor.class.getName());
22+
23+
private ServerSocket serverSocket = null;
24+
25+
public RequestProcessor(final ServerSocket ssock) {
26+
super();
27+
this.serverSocket = ssock;
28+
}
29+
30+
31+
public void handleClientRequest() throws IOException {
32+
try (Socket client = serverSocket.accept()) {
33+
logger.info("Processing request from client " + client.getInetAddress().getHostAddress());
34+
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
35+
36+
StringBuilder requestBuilder = new StringBuilder();
37+
String line;
38+
while (!(line = br.readLine()).isBlank()) {
39+
requestBuilder.append(line + "\r\n");
40+
}
41+
42+
String request = requestBuilder.toString();
43+
String[] requestsLines = request.split("\r\n");
44+
String[] requestLine = requestsLines[0].split(" ");
45+
String method = requestLine[0];
46+
String path = requestLine[1];
47+
String version = requestLine[2];
48+
String host = requestsLines[1].split(" ")[1];
49+
50+
List<String> headers = new ArrayList<>();
51+
for (int h = 2; h < requestsLines.length; h++) {
52+
String header = requestsLines[h];
53+
headers.add(header);
54+
}
55+
56+
String accessLog = String.format("Client %s, method %s, path %s, version %s, host %s, headers %s",
57+
client.toString(), method, path, version, host, headers.toString());
58+
System.out.println(accessLog);
59+
60+
String contentType = "application/json";
61+
String dummyContent = "{\"message\":\"This is a dummy server. I am healthy\"}";
62+
sendResponse(client, "200 OK", contentType, dummyContent.getBytes());
63+
} catch (Exception e) {
64+
logger.info("Error in processing "+e.getMessage());
65+
}
66+
67+
}
68+
69+
private void sendResponse(Socket client, String status, String contentType, byte[] content) throws IOException {
70+
OutputStream clientOutput = client.getOutputStream();
71+
clientOutput.write(("HTTP/1.1 \r\n" + status).getBytes());
72+
clientOutput.write(("ContentType: " + contentType + "\r\n").getBytes());
73+
clientOutput.write("\r\n".getBytes());
74+
clientOutput.write(content);
75+
clientOutput.write("\r\n\r\n".getBytes());
76+
clientOutput.flush();
77+
client.close();
78+
}
79+
80+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
*
3+
*/
4+
package io.pratik.threadops;
5+
6+
import java.lang.management.ManagementFactory;
7+
import java.lang.management.ThreadInfo;
8+
import java.lang.management.ThreadMXBean;
9+
import java.util.logging.Logger;
10+
11+
/**
12+
* @author pratikdas
13+
*
14+
*/
15+
public class ThreadMXBeanSample {
16+
private static final Logger logger = Logger.getLogger(ThreadMXBeanSample.class.getName());
17+
18+
public static void main(String[] args) {
19+
startThreads();
20+
ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
21+
for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
22+
23+
logger.info(ti.toString());
24+
}
25+
26+
logger.info("\nGeneral Thread information");
27+
logger.info(("Number of live threads :" + threadMxBean.getThreadCount()));
28+
logger.info("Total CPU time for the current thread: " + threadMxBean.getCurrentThreadCpuTime());
29+
logger.info("Current number of live daemon threads:" + threadMxBean.getDaemonThreadCount());
30+
logger.info("Peak live thread count :" + threadMxBean.getPeakThreadCount());
31+
logger.info("Total number of threads created and started : " + threadMxBean.getTotalStartedThreadCount());
32+
}
33+
34+
/**
35+
* Starts two threads thread1 and thread2 and calls their synchronized methods
36+
* in the run method resulting in a deadlock.
37+
*/
38+
private static void startThreads() {
39+
final ThreadSample thread1 = new ThreadSample();
40+
final ThreadSample thread2 = new ThreadSample();
41+
Thread t1 = new Thread("Thread1") {
42+
public void run() {
43+
thread1.executeMethod1(thread2);
44+
}
45+
};
46+
47+
Thread t2 = new Thread("Thread2") {
48+
@Override
49+
public void run() {
50+
thread2.executeMethod2(thread1);
51+
}
52+
};
53+
54+
t1.start();
55+
t2.start();
56+
}
57+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
*
3+
*/
4+
package io.pratik.threadops;
5+
6+
import java.util.logging.Logger;
7+
8+
/**
9+
* @author pratikdas
10+
*
11+
*/
12+
public class ThreadSample {
13+
private static final Logger logger = Logger.getLogger(ThreadSample.class.getName());
14+
15+
synchronized void executeMethod1(final ThreadSample thread) {
16+
log("Thread " + Thread.currentThread().getName() + " is executing");
17+
thread.executeMethod2(this);
18+
log(Thread.currentThread().getName() + " has finished method execution");
19+
}
20+
21+
synchronized void executeMethod2(final ThreadSample thread) {
22+
log("Thread " + Thread.currentThread().getName() + " is executing");
23+
thread.executeMethod1(this);
24+
log(Thread.currentThread().getName() + " has finished method execution");
25+
}
26+
27+
private void log(final String message) {
28+
System.out.println(message);
29+
//logger.info(message);
30+
}
31+
32+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.pratik;
2+
3+
import junit.framework.Test;
4+
import junit.framework.TestCase;
5+
import junit.framework.TestSuite;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
extends TestCase
12+
{
13+
/**
14+
* Create the test case
15+
*
16+
* @param testName name of the test case
17+
*/
18+
public AppTest( String testName )
19+
{
20+
super( testName );
21+
}
22+
23+
/**
24+
* @return the suite of tests being tested
25+
*/
26+
public static Test suite()
27+
{
28+
return new TestSuite( AppTest.class );
29+
}
30+
31+
/**
32+
* Rigourous Test :-)
33+
*/
34+
public void testApp()
35+
{
36+
assertTrue( true );
37+
}
38+
}

0 commit comments

Comments
 (0)