Follow up---resource contention consideration


  • 0
    J
    private Map<String, Integer> map;
    private Object obj;
    /** Initialize your data structure here. */
    public Logger() {
        map = new HashMap();
        obj = new Object();
    }
    
    /** Returns true if the message should be printed in the given timestamp, otherwise returns false.
        If this method returns false, the message will not be printed.
        The timestamp is in seconds granularity. */
    public boolean shouldPrintMessage(int timestamp, String message) {
        synchronized(obj){
            Integer time = map.get(message);
            boolean res = time == null || (timestamp - time) >= 10;
            if(res){
                map.put(message, timestamp);
            }
            return res;
        }
      
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.