Skip to content

Commit 21d950d

Browse files
authored
Merge pull request #22 from evidentart/new-demo
threads practice
2 parents b8e2402 + df12ffd commit 21d950d

File tree

10 files changed

+193
-0
lines changed

10 files changed

+193
-0
lines changed

threads/.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "interactive"
3+
}

threads/pom.xml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>umbc</groupId>
8+
<artifactId>threads</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>threads</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>17</maven.compiler.source>
18+
<maven.compiler.target>17</maven.compiler.target>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<version>4.11</version>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
32+
<plugins>
33+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
34+
<plugin>
35+
<artifactId>maven-clean-plugin</artifactId>
36+
<version>3.1.0</version>
37+
</plugin>
38+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
39+
<plugin>
40+
<artifactId>maven-resources-plugin</artifactId>
41+
<version>3.0.2</version>
42+
</plugin>
43+
<plugin>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>3.8.0</version>
46+
</plugin>
47+
<plugin>
48+
<artifactId>maven-surefire-plugin</artifactId>
49+
<version>2.22.1</version>
50+
</plugin>
51+
<plugin>
52+
<artifactId>maven-jar-plugin</artifactId>
53+
<version>3.0.2</version>
54+
</plugin>
55+
<plugin>
56+
<artifactId>maven-install-plugin</artifactId>
57+
<version>2.5.2</version>
58+
</plugin>
59+
<plugin>
60+
<artifactId>maven-deploy-plugin</artifactId>
61+
<version>2.8.2</version>
62+
</plugin>
63+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
64+
<plugin>
65+
<artifactId>maven-site-plugin</artifactId>
66+
<version>3.7.1</version>
67+
</plugin>
68+
<plugin>
69+
<artifactId>maven-project-info-reports-plugin</artifactId>
70+
<version>3.0.0</version>
71+
</plugin>
72+
</plugins>
73+
</pluginManagement>
74+
</build>
75+
</project>

threads/src/main/java/umbc/App.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package umbc;
2+
3+
4+
5+
public class App
6+
{
7+
public static void main( String[] args ) throws InterruptedException
8+
{
9+
// without threads it takes about 20 seconds to complete
10+
// System.out.println( Thread.currentThread().getName());
11+
12+
// for (int i = 0; i < 20; i++) {
13+
// System.out.println("Practice coding");
14+
// Thread.sleep(500);
15+
// }
16+
17+
// for (int i = 0; i < 20; i++) {
18+
// System.out.println("Play outside");
19+
// Thread.sleep(500);
20+
// }
21+
22+
// with threads it takes about 10 seconds to completes
23+
// practice coding thread
24+
Thread practiceCoding = new PracticeCodingThread();
25+
// practice coding thread
26+
Thread playOutside = new Thread(new PlayOutsideThread());
27+
28+
29+
System.out.println("Practice coding state: " + practiceCoding.getState());
30+
System.out.println("Play outside state: " + playOutside.getState());
31+
System.out.println("Main outside state: " + Thread.currentThread().getState());
32+
33+
// // using lambda expression
34+
// Thread anotherThing = new Thread(() -> {
35+
36+
// for (int i = 0; i < 20; i++) {
37+
// System.out.println("Practice coding");
38+
// try {
39+
// Thread.sleep(500);
40+
// } catch (InterruptedException e) {
41+
// e.printStackTrace();
42+
// }
43+
// }
44+
// });
45+
46+
practiceCoding.start();
47+
playOutside.start();
48+
49+
// wait for threads to complete
50+
while (practiceCoding.isAlive() || playOutside.isAlive()) {
51+
Thread.sleep(500);
52+
System.out.println("Practice coding state: " + practiceCoding.getState());
53+
System.out.println("Play outside state: " + playOutside.getState());
54+
}
55+
56+
57+
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package umbc;
2+
3+
public class PlayOutsideThread implements Runnable {
4+
5+
@Override
6+
public void run() {
7+
8+
try {
9+
for (int j = 0; j < 20; j++) {
10+
System.out.println("Play outside");
11+
Thread.sleep(500);
12+
}
13+
} catch (Exception e) {
14+
e.printStackTrace();
15+
}
16+
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package umbc;
2+
3+
public class PracticeCodingThread extends Thread {
4+
5+
@Override
6+
public void run() {
7+
System.out.println("Practice coding state: " + getState());
8+
try {
9+
for (int j = 0; j < 20; j++) {
10+
System.out.println("Practice coding");
11+
Thread.sleep(500);
12+
}
13+
} catch (Exception e) {
14+
e.printStackTrace();
15+
}
16+
17+
}
18+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package umbc;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
{
12+
/**
13+
* Rigorous Test :-)
14+
*/
15+
@Test
16+
public void shouldAnswerWithTrue()
17+
{
18+
assertTrue( true );
19+
}
20+
}

threads/target/classes/umbc/App.class

1.81 KB
Binary file not shown.
800 Bytes
Binary file not shown.
Binary file not shown.
455 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)