Skip to content

Commit 0158d7f

Browse files
committed
Day-17 : Hacker Police Game.
1 parent 5986f8c commit 0158d7f

3 files changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.java8;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Stream;
6+
7+
public class StreamReuse {
8+
//We can't resue stream.
9+
public static void main(String[] args) {
10+
List<String> nameList = Arrays.asList("alice", "neena", "meena", "rohan", "neema");
11+
Stream<String> stringStream = nameList.stream();
12+
//Consumption 1
13+
stringStream.forEach(System.out::println);
14+
//Consumption 2 will throw IllegalStateException stating stream has already been operated upon or closed
15+
// long length = stringStream.count();
16+
17+
}
18+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.multithreading;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
7+
public class HackerPolice {
8+
private static final int MAX_PASSWORD = 9999;
9+
10+
public static void main(String[] args) {
11+
Random random = new Random();
12+
Vault vault = new Vault(random.nextInt(MAX_PASSWORD));
13+
List<Thread> threads = new ArrayList<>();
14+
threads.add(new AscendingHackerThread(vault));
15+
threads.add(new DescendingHackerThread(vault));
16+
threads.add(new PoliceThread());
17+
for (Thread thread : threads) {
18+
thread.start();
19+
}
20+
21+
}
22+
23+
private static class Vault {
24+
private int password;
25+
26+
public Vault(int password) {
27+
this.password = password;
28+
}
29+
30+
31+
public boolean isCorrectPassword(int guess) {
32+
try {
33+
Thread.sleep(5);
34+
} catch (InterruptedException e) {
35+
}
36+
return this.password == guess;
37+
}
38+
}
39+
40+
private static abstract class HackerThread extends Thread {
41+
protected Vault vault;
42+
43+
public HackerThread(Vault vault) {
44+
this.vault = vault;
45+
this.setName(this.getClass().getSimpleName());
46+
this.setPriority(Thread.MAX_PRIORITY);
47+
}
48+
49+
@Override
50+
public void start() {
51+
System.out.println("Starting thread" + this.getName());
52+
super.start();
53+
}
54+
}
55+
56+
private static class AscendingHackerThread extends HackerThread {
57+
public AscendingHackerThread(Vault vault) {
58+
super(vault);
59+
}
60+
61+
@Override
62+
public void run() {
63+
for (int guess = 0; guess < MAX_PASSWORD; guess++) {
64+
if (vault.isCorrectPassword(guess)) {
65+
System.out.println(this.getName() + " get the password " + guess);
66+
System.exit(0);
67+
}
68+
}
69+
}
70+
}
71+
72+
private static class DescendingHackerThread extends HackerThread {
73+
public DescendingHackerThread(Vault vault) {
74+
super(vault);
75+
}
76+
77+
@Override
78+
public void run() {
79+
for (int guess = MAX_PASSWORD; guess >= 0; guess--) {
80+
if (vault.isCorrectPassword(guess)) {
81+
System.out.println(this.getName() + " get the password " + guess);
82+
System.exit(0);
83+
}
84+
}
85+
}
86+
}
87+
88+
private static class PoliceThread extends Thread {
89+
@Override
90+
public void run() {
91+
for (int i = 10; i > 0; i--) {
92+
try {
93+
Thread.sleep(1000);
94+
} catch (InterruptedException e) {
95+
}
96+
System.out.println(i);
97+
}
98+
System.out.println("Game over for you hackers!");
99+
System.exit(0);
100+
}
101+
}
102+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.multithreading;
2+
3+
public class ThreadCreation {
4+
public static void main(String[] args) throws InterruptedException {
5+
Thread thread = new Thread(new Runnable() {
6+
@Override
7+
public void run() {
8+
System.out.println("Thread name inside run method: " + Thread.currentThread().getName());
9+
System.out.println("Priority of this thread is: " + Thread.currentThread().getPriority());
10+
}
11+
});
12+
thread.setPriority(Thread.MAX_PRIORITY);
13+
thread.setName("New Worker Thread");
14+
System.out.println("Thread Name before start: " + Thread.currentThread().getName());
15+
thread.start();
16+
System.out.println("Thread name after start: " + Thread.currentThread().getName());
17+
Thread.sleep(1000);
18+
}
19+
}

0 commit comments

Comments
 (0)