thread dumo example
This commit is contained in:
47
core-java/threaddump/pom.xml
Normal file
47
core-java/threaddump/pom.xml
Normal file
@@ -0,0 +1,47 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>io.pratik.server</groupId>
|
||||
<artifactId>ServerApp</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>ServerApp</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>11</source>
|
||||
<target>11</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<!-- Build an executable JAR -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>io.pratik.server.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
21
core-java/threaddump/src/main/java/io/pratik/server/App.java
Normal file
21
core-java/threaddump/src/main/java/io/pratik/server/App.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package io.pratik.server;
|
||||
|
||||
import java.net.ServerSocket;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author pratikdas
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
private static final Logger logger = Logger.getLogger(App.class.getName());
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ServerSocket ssock = new ServerSocket(8080);
|
||||
logger.info("Server Started. Listening on port 8080");
|
||||
|
||||
while (true) {
|
||||
new RequestProcessor(ssock).handleClientRequest();;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package io.pratik.server;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author pratikdas
|
||||
*
|
||||
*/
|
||||
public class RequestProcessor {
|
||||
private static final Logger logger = Logger.getLogger(RequestProcessor.class.getName());
|
||||
|
||||
private ServerSocket serverSocket = null;
|
||||
|
||||
public RequestProcessor(final ServerSocket ssock) {
|
||||
super();
|
||||
this.serverSocket = ssock;
|
||||
}
|
||||
|
||||
|
||||
public void handleClientRequest() throws IOException {
|
||||
try (Socket client = serverSocket.accept()) {
|
||||
logger.info("Processing request from client " + client.getInetAddress().getHostAddress());
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
||||
|
||||
StringBuilder requestBuilder = new StringBuilder();
|
||||
String line;
|
||||
while (!(line = br.readLine()).isBlank()) {
|
||||
requestBuilder.append(line + "\r\n");
|
||||
}
|
||||
|
||||
String request = requestBuilder.toString();
|
||||
String[] requestsLines = request.split("\r\n");
|
||||
String[] requestLine = requestsLines[0].split(" ");
|
||||
String method = requestLine[0];
|
||||
String path = requestLine[1];
|
||||
String version = requestLine[2];
|
||||
String host = requestsLines[1].split(" ")[1];
|
||||
|
||||
List<String> headers = new ArrayList<>();
|
||||
for (int h = 2; h < requestsLines.length; h++) {
|
||||
String header = requestsLines[h];
|
||||
headers.add(header);
|
||||
}
|
||||
|
||||
String accessLog = String.format("Client %s, method %s, path %s, version %s, host %s, headers %s",
|
||||
client.toString(), method, path, version, host, headers.toString());
|
||||
System.out.println(accessLog);
|
||||
|
||||
String contentType = "application/json";
|
||||
String dummyContent = "{\"message\":\"This is a dummy server. I am healthy\"}";
|
||||
sendResponse(client, "200 OK", contentType, dummyContent.getBytes());
|
||||
} catch (Exception e) {
|
||||
logger.info("Error in processing "+e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void sendResponse(Socket client, String status, String contentType, byte[] content) throws IOException {
|
||||
OutputStream clientOutput = client.getOutputStream();
|
||||
clientOutput.write(("HTTP/1.1 \r\n" + status).getBytes());
|
||||
clientOutput.write(("ContentType: " + contentType + "\r\n").getBytes());
|
||||
clientOutput.write("\r\n".getBytes());
|
||||
clientOutput.write(content);
|
||||
clientOutput.write("\r\n\r\n".getBytes());
|
||||
clientOutput.flush();
|
||||
client.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package io.pratik.threadops;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.ThreadInfo;
|
||||
import java.lang.management.ThreadMXBean;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author pratikdas
|
||||
*
|
||||
*/
|
||||
public class ThreadMXBeanSample {
|
||||
private static final Logger logger = Logger.getLogger(ThreadMXBeanSample.class.getName());
|
||||
|
||||
public static void main(String[] args) {
|
||||
startThreads();
|
||||
ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
|
||||
for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
|
||||
|
||||
logger.info(ti.toString());
|
||||
}
|
||||
|
||||
logger.info("\nGeneral Thread information");
|
||||
logger.info(("Number of live threads :" + threadMxBean.getThreadCount()));
|
||||
logger.info("Total CPU time for the current thread: " + threadMxBean.getCurrentThreadCpuTime());
|
||||
logger.info("Current number of live daemon threads:" + threadMxBean.getDaemonThreadCount());
|
||||
logger.info("Peak live thread count :" + threadMxBean.getPeakThreadCount());
|
||||
logger.info("Total number of threads created and started : " + threadMxBean.getTotalStartedThreadCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts two threads thread1 and thread2 and calls their synchronized methods
|
||||
* in the run method resulting in a deadlock.
|
||||
*/
|
||||
private static void startThreads() {
|
||||
final ThreadSample thread1 = new ThreadSample();
|
||||
final ThreadSample thread2 = new ThreadSample();
|
||||
Thread t1 = new Thread("Thread1") {
|
||||
public void run() {
|
||||
thread1.executeMethod1(thread2);
|
||||
}
|
||||
};
|
||||
|
||||
Thread t2 = new Thread("Thread2") {
|
||||
@Override
|
||||
public void run() {
|
||||
thread2.executeMethod2(thread1);
|
||||
}
|
||||
};
|
||||
|
||||
t1.start();
|
||||
t2.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package io.pratik.threadops;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author pratikdas
|
||||
*
|
||||
*/
|
||||
public class ThreadSample {
|
||||
private static final Logger logger = Logger.getLogger(ThreadSample.class.getName());
|
||||
|
||||
synchronized void executeMethod1(final ThreadSample thread) {
|
||||
log("Thread " + Thread.currentThread().getName() + " is executing");
|
||||
thread.executeMethod2(this);
|
||||
log(Thread.currentThread().getName() + " has finished method execution");
|
||||
}
|
||||
|
||||
synchronized void executeMethod2(final ThreadSample thread) {
|
||||
log("Thread " + Thread.currentThread().getName() + " is executing");
|
||||
thread.executeMethod1(this);
|
||||
log(Thread.currentThread().getName() + " has finished method execution");
|
||||
}
|
||||
|
||||
private void log(final String message) {
|
||||
System.out.println(message);
|
||||
//logger.info(message);
|
||||
}
|
||||
|
||||
}
|
||||
38
core-java/threaddump/src/test/java/io/pratik/AppTest.java
Normal file
38
core-java/threaddump/src/test/java/io/pratik/AppTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package io.pratik;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user