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 d = new Thread(new Desarmer(kode)); Thread b = new Thread(new Bombe(kode)); d.start(); b.start(); try { d.join(); } catch (InterruptedException e) { } b.interrupt(); } } class Kode { String kode; Lock lock = new ReentrantLock(); boolean sammenlignKode(String kod) { return kode.equals(kod); } void settKode(String kod) { lock.lock(); try { kode = kod; } finally { lock.unlock(); } } public String hentKode() { return kode; } } class Bombe implements Runnable{ Kode kode; Bombe(Kode kode) { this.kode = kode; kode.settKode("hei"); } @Override public void run() { try { for (int i = 0; i < 5; i++) { System.out.println("Tikk!"); Thread.sleep(1000); } } catch (InterruptedException e) { return; } System.out.println("BOOOOM"); System.exit(0); } } class Desarmer implements Runnable{ Scanner sc = new Scanner(System.in); Kode kode; Desarmer(Kode kod) { kode = kod; } @Override public void run() { System.out.println("Hvis du ikke skriver inn " + kode.hentKode() + " innen 5 sekunder, sprenger bomben!!!!"); String inn = sc.nextLine(); while(!kode.sammenlignKode(inn)) { System.out.println("Feil input"); inn = sc.nextLine(); } System.out.println("Du klarte det!!!"); } }