public static void lock(long t) {
        int tId = (int) t;
        choosing.set(tId, 1);
        ticket.set(tId, nextTicket());
        choosing.set(tId, 0);
        for (int i = 0; i < ticket.length(); i++) {
            if (i != tId) {
                while (choosing.get(i) == 1) { Thread.yield(); }
                while (ticket.get(i) != 0 &&
                        (ticket.get(tId) > ticket.get(i) ||
                                (ticket.get(tId) == ticket.get(i) && tId > i))) {
                    Thread.yield();
                }
            }
        }
    }