Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

42
LINES

< > BotCompany Repo | #1000782 // Simple "Flag" class (thread-safe flagging!)

JavaX fragment (include) [tags: archive]

1  
/** this class is fully thread-safe */
2  
class Flag {
3  
  private boolean up;
4  
5  
  /** returns true if flag was down before */
6  
  public synchronized boolean raise() {
7  
    return doRaise();
8  
  }
9  
10  
  private synchronized boolean doRaise() {
11  
    if (!up) {
12  
      up = true;
13  
      notifyAll();
14  
      return true;
15  
    } else
16  
      return false;
17  
  }
18  
19  
  public synchronized void waitUntilUp() {
20  
    if (!up) {
21  
      try {
22  
        wait();
23  
      } catch (InterruptedException e) {
24  
        e.printStackTrace();
25  
      }
26  
    }
27  
  }
28  
29  
  public synchronized boolean isUp() {
30  
    return up;
31  
  }
32  
33  
  public String toString() {
34  
    return isUp() ? "up" : "down";
35  
  }
36  
37  
  // currently does a semi-active wait with latency = 50 ms
38  
  public void waitForThisOr(Flag otherFlag) ctex {
39  
    while (!isUp() && !otherFlag.isUp())
40  
      Thread.sleep(50);
41  
  }
42  
}

Author comment

Began life as a copy of #1000781

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): cfunsshuasjs, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1000782
Snippet name: Simple "Flag" class (thread-safe flagging!)
Eternal ID of this version: #1000782/1
Text MD5: d4ed5418a5fde2cc3629ea0cddca98f7
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): Yes
Created/modified: 2015-08-27 02:00:08
Source code size: 862 bytes / 42 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 448 / 619
Referenced in: [show references]