rt

An obligatory Java Project
Log | Files | Refs | LICENSE

commit 7e024f159c389ea4dc4cddaa90d1e238ea25bd21
parent 6a8c2f64a9efd177862df5af1dd5d951e03a5817
Author: zerous Naveen Narayanan <zerous@nocebo.space>
Date:   Mon, 25 Nov 2019 16:43:10 +0100

Implement methods and use locks

resetUsers()
getAssignedUsers()
assignUser()
getResult()

Diffstat:
Mscript.java | 98++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 92 insertions(+), 6 deletions(-)

diff --git a/script.java b/script.java @@ -1,21 +1,107 @@ package space.nocebo; +import java.util.concurrent.locks.*; + public class script { private int dlevel; - private String[] roles = new String[3]; + private String[][] roles = new String[3][3]; public script(String c1, String c2, String c3, int d) { dlevel = d; - roles[0] = c1; - roles[1] = c2; - roles[2] = c3; + roles[0][0] = c1; + roles[1][0] = c2; + roles[2][0] = c3; + roles[0][1] = ""; + roles[1][1] = ""; + roles[2][1] = ""; + roles[0][2] = ""; + roles[1][2] = ""; + roles[2][2] = ""; } - public int getDlevel() { + public synchronized int getDlevel() { return dlevel; } - public String[] getRoles() { + public synchronized String[][] getRoles() { return roles; } + + public synchronized void resetUsers() { + for (int i = 0, j = 0; i < 3; i++) + roles[i][j] = ""; + } + + public String getAssignedUsers(Lock lock) { + lock.lock(); + try { + String s = ""; + for (int i = 0, j = 1; i < 3; i++) + if ((roles[i][j].isEmpty() == false)) { + s += roles[i][j] + " assigned to " + roles[i][0] + "\n"; + } + return s; + } finally { + lock.unlock(); + } + } + + public boolean assignUser(int p, acc a, Lock lock) { + System.out.println("prior locked" + a.getUname()); + lock.lock(); + System.out.println("locked"); + try { + System.out.println("dbg: assignUser " + p + " " + a.getUname()); + a.setScore(); + if (p == 0) + { + for (int i = 0, j = 1; i < 3; i++) { + if (roles[i][j].isEmpty() == true) { + roles[i][j] = a.getUname(); + roles[i][j+1] = Integer.toString(a.getScore()); + System.out.println("dbg: " + roles[i][0] + a.getUname() + roles[i][2]); + return true; + } + } + } else if (p > 0) { + if (roles[p - 1][1].isEmpty() == true) { + roles[p - 1][1] = a.getUname(); + roles[p - 1][2] = Integer.toString(a.getScore()); + return true; + } + } + return false; + } finally { + lock.unlock(); + System.out.println("Unlocked"); + } + } + + public String getResult(Lock lock, acc a) { + System.out.println("getResult: prior lock " + a.getUname()); + lock.lock(); + System.out.println("getResult: locked " + a.getUname()); + String s = ""; + try { + while (true) { + if (roles[0][1].isEmpty() || roles[1][1].isEmpty() || roles[2][1].isEmpty()) { + try { + Thread.sleep(2000); + } catch (InterruptedException ex) { + System.out.println("InterruptedException " + ex); + } + } + else + break; + } + s = roles[0][0] + " played by " + roles[0][1] + " got " + roles[0][2] + " points." + "\n" + + roles[1][0] + " played by " + roles[1][1] + " got " + roles[1][2] + " points." + "\n" + + roles[2][0] + " played by " + roles[2][1] + " got " + roles[2][2] + " points." + "\n"; + System.out.println("getResult s: " + s); + return s; + } finally { + lock.unlock(); + System.out.println("getResult: unlocked " + a.getUname()); + } + } }