|
| 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 | +} |
0 commit comments