import java.util.Scanner; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Bombeprogram { public static void main(String[] args) { Kode kode = new Kode(); Thread bombe = new Thread(new Bombe(kode)); Thread defuser = new Thread(new Desarmer(kode)); bombe.start(); bombe.interrupt(); //defuser.start(); try { defuser.join(); } catch (InterruptedException e) {} } } class Bombe implements Runnable { private final int START = 50000000; private int teller = START; private Kode kode; public Bombe(Kode k) { this.kode = k; System.out.println("Legg inn armeringskode: "); kode.settInnKode(new Scanner(System.in).nextLine()); } public void run() { while (teller > 0) { try { System.out.println(teller); teller--; Thread.sleep(1000); } catch (InterruptedException e) {return;} } System.out.println("BOOOOM!"); System.exit(0); } } class Desarmer implements Runnable { Scanner input = new Scanner(System.in); Kode kode; public Desarmer(Kode k) { this.kode = k; } public void run() { System.out.println("Desarmer bomben! Koden er " + kode.kode()); String forsok = input.nextLine(); while (!kode.erLike(forsok)) { System.out.println("Det var feil!"); forsok = input.nextLine(); } System.out.println("Phew!"); } } class Kode { private Lock laas = new ReentrantLock(); private String kode; public void settInnKode(String k) { laas.lock(); try { kode = k; } finally { laas.unlock(); } } public String kode() { return kode; } public boolean erLike(String testKode) { return testKode.equals(kode); } }