Skip to content

Commit 5e30e6c

Browse files
committed
Adding High CPU consuming Java Program. Configured Travis. Updated .gitignore and README.md
1 parent 4c5327f commit 5e30e6c

File tree

9 files changed

+518
-4
lines changed

9 files changed

+518
-4
lines changed

.gitignore

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
*.class
22

3-
# Mobile Tools for Java (J2ME)
4-
.mtj.tmp/
5-
63
# Package Files #
74
*.jar
85
*.war
96
*.ear
107

118
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
129
hs_err_pid*
10+
11+
#Maven Target
12+
target/
13+
14+
#Eclipse files
15+
.project
16+
.classpath
17+
18+
tmp/
19+
*.tmp
20+
*.bak
21+
*.swp
22+
*~.nib
23+
.settings/
24+
# Locally stored "Eclipse launch configurations"
25+
*.launch
26+
27+
# Flight Recorder files
28+
*.jfr
29+
30+
# Log files
31+
*.log

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: java
2+
jdk:
3+
- oraclejdk8

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1-
# sample-java-programs
1+
[![Build Status](https://travis-ci.org/chrishantha/sample-java-programs.svg?branch=master)](https://travis-ci.org/chrishantha/sample-java-programs)
2+
23
Sample Java Programs
4+
====================
5+
6+
This repository contains some sample programs. All are Maven projects and can be run directly using "java -jar"
7+
8+
9+
## Java Programs
10+
11+
**highcpu**
12+
13+
A java program consuming high CPU usage. This program was inspired by a sample found in the article "[Identifying which Java Thread is consuming most CPU](http://code.nomad-labs.com/2010/11/18/identifying-which-java-thread-is-consuming-most-cpu/)"
14+
15+
## License
16+
17+
Copyright (C) 2015 M. Isuru Tharanga Chrishantha Perera
18+
19+
Licensed under the Apache License, Version 2.0

highcpu/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
# Copyright 2015 M. Isuru Tharanga Chrishantha Perera
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
20+
<parent>
21+
<groupId>com.github.chrishantha.sample</groupId>
22+
<artifactId>java-samples</artifactId>
23+
<version>0.0.1-SNAPSHOT</version>
24+
</parent>
25+
26+
<modelVersion>4.0.0</modelVersion>
27+
<artifactId>highcpu</artifactId>
28+
<packaging>jar</packaging>
29+
<name>highcpu</name>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>com.beust</groupId>
34+
<artifactId>jcommander</artifactId>
35+
</dependency>
36+
</dependencies>
37+
38+
39+
<properties>
40+
<fully.qualified.main.class>com.github.chrishantha.sample.highcpu.App</fully.qualified.main.class>
41+
</properties>
42+
</project>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2015 M. Isuru Tharanga Chrishantha Perera
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.chrishantha.sample.highcpu;
17+
18+
import java.util.Timer;
19+
import java.util.TimerTask;
20+
21+
import com.beust.jcommander.JCommander;
22+
import com.beust.jcommander.Parameter;
23+
24+
public class App {
25+
26+
@Parameter(names = "--run-hashing", description = "Run Hashing Worker", arity = 1)
27+
private boolean runHashing = true;
28+
29+
@Parameter(names = "--run-sleeping", description = "Run Sleeping Worker", arity = 1)
30+
private boolean runSleeping = true;
31+
32+
@Parameter(names = "--run-math", description = "Run Math Worker", arity = 1)
33+
private boolean runMath = true;
34+
35+
@Parameter(names = "--hashing-workers", description = "Hashing Workers")
36+
private int hashingWorkers = 1;
37+
38+
@Parameter(names = "--sleeping-workers", description = "Sleeping Workers")
39+
private int sleepingWorkers = 3;
40+
41+
@Parameter(names = "--math-workers", description = "Math Workers")
42+
private int mathWorkers = 2;
43+
44+
@Parameter(names = "--hash-data-length", description = "Hash Data Length")
45+
private long hashDataLength = 2000;
46+
47+
@Parameter(names = "--hashing-algo", description = "Hashing Algorithm")
48+
private String hashingAlgorithm = "SHA-1";
49+
50+
@Parameter(names = "--help", description = "Display Help", help = true)
51+
private boolean help;
52+
53+
@Parameter(names = "--exit-timeout", description = "Exit Timeout in Seconds (Default 2 minutes. Use 0 to run forever)")
54+
private int exitTimeoutInSeconds = 2 * 60;
55+
56+
public static void main(String[] args) {
57+
App app = new App();
58+
final JCommander jcmdr = new JCommander(app);
59+
jcmdr.setProgramName(App.class.getSimpleName());
60+
jcmdr.parse(args);
61+
62+
if (app.help) {
63+
jcmdr.usage();
64+
return;
65+
}
66+
67+
app.start();
68+
}
69+
70+
private void start() {
71+
System.out.println("Starting Application...");
72+
if (exitTimeoutInSeconds > 0) {
73+
final Timer timer = new Timer();
74+
timer.schedule(new TimerTask() {
75+
public void run() {
76+
System.out.println("Exiting now...");
77+
System.exit(0);
78+
}
79+
}, exitTimeoutInSeconds * 1000);
80+
}
81+
if (runHashing) {
82+
for (int i = 0; i < hashingWorkers; i++) {
83+
startThread(i, "Hashing", new HashingWorker(hashDataLength, hashingAlgorithm));
84+
}
85+
}
86+
if (runSleeping) {
87+
for (int i = 0; i < sleepingWorkers; i++) {
88+
startThread(i, "Sleeping", new SleepingWorker());
89+
}
90+
}
91+
if (runMath) {
92+
for (int i = 0; i < mathWorkers; i++) {
93+
startThread(i, "Math", new MathWorker());
94+
}
95+
}
96+
}
97+
98+
private void startThread(int i, String name, Runnable worker) {
99+
Thread thread = new Thread(new HashingWorker(hashDataLength, hashingAlgorithm));
100+
thread.setName(String.format("Thread %d: %s", i, name));
101+
thread.start();
102+
}
103+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2015 M. Isuru Tharanga Chrishantha Perera
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.chrishantha.sample.highcpu;
17+
18+
import java.security.MessageDigest;
19+
import java.security.NoSuchAlgorithmException;
20+
import java.util.UUID;
21+
22+
public class HashingWorker implements Runnable {
23+
24+
private final String algorithm;
25+
private final long length;
26+
27+
public HashingWorker(long length, String algorithm) {
28+
this.length = length;
29+
this.algorithm = algorithm;
30+
}
31+
32+
@Override
33+
public void run() {
34+
while (true) {
35+
String data = "";
36+
37+
// Some random data
38+
for (int i = 0; i < length; i++) {
39+
data += UUID.randomUUID().toString();
40+
}
41+
42+
MessageDigest digest;
43+
try {
44+
digest = MessageDigest.getInstance(algorithm);
45+
} catch (NoSuchAlgorithmException e) {
46+
throw new RuntimeException(e);
47+
}
48+
49+
// Hash
50+
digest.update(data.getBytes());
51+
}
52+
}
53+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2015 M. Isuru Tharanga Chrishantha Perera
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.chrishantha.sample.highcpu;
17+
18+
import java.math.BigDecimal;
19+
import java.util.Random;
20+
21+
public class MathWorker implements Runnable {
22+
23+
private Random random = new Random();
24+
25+
public MathWorker() {
26+
}
27+
28+
@Override
29+
public void run() {
30+
while (true) {
31+
BigDecimal sum = BigDecimal.ZERO;
32+
for (int i = 0; i <= Integer.MAX_VALUE; i++) {
33+
double value = Math.atan(Math.sqrt(Math.pow(random.nextInt(10), random.nextDouble())));
34+
sum = sum.add(BigDecimal.valueOf(value));
35+
}
36+
}
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2015 M. Isuru Tharanga Chrishantha Perera
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.chrishantha.sample.highcpu;
17+
18+
import java.util.Random;
19+
20+
public class SleepingWorker implements Runnable {
21+
22+
private Random random = new Random();
23+
24+
public SleepingWorker() {
25+
}
26+
27+
@Override
28+
public void run() {
29+
Long l = 0l;
30+
while (true) {
31+
l++;
32+
try {
33+
Thread.sleep(random.nextInt(20));
34+
} catch (InterruptedException e) {
35+
e.printStackTrace();
36+
}
37+
if (l == Long.MAX_VALUE) {
38+
l = 0l;
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)