Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package stringquiz;
- import java.util.SortedMap;
- import java.util.TreeMap;
- /**
- *
- * @author alex
- */
- public class StringQuiz {
- private String segreta;
- private long timeOut;
- private static SortedMap<Thread,Integer> mappatura;
- public StringQuiz(String s, long durata){
- this.segreta = s;
- this.timeOut = System.currentTimeMillis() + durata;
- mappatura = new TreeMap<Thread,Integer>();
- }
- public synchronized boolean guess(String prova) throws Exception{
- if(System.currentTimeMillis()>=timeOut)
- throw new Exception("tempo scarduto! ");
- if(!mappatura.containsKey(Thread.currentThread()))
- mappatura.put(Thread.currentThread(),0);
- else{
- if(mappatura.get(Thread.currentThread())==3)
- throw new Exception("Il thread ci ha già provato 3 volte!");
- else
- if(prova.equals(segreta)==true)
- return true;
- else
- mappatura.replace(Thread.currentThread(), mappatura.get(Thread.currentThread())+1);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement