forked from google/thread-weaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserCache.java
More file actions
66 lines (56 loc) · 1.71 KB
/
UserCache.java
File metadata and controls
66 lines (56 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright 2009 Weaver authors
*
* This code is part of the Weaver tutorial and may be freely used.
*/
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Caches an avatar corresponding to a given user. This is a test class,
* used to illustrate issues with read-write locks. See class example for
* {@link java.util.concurrent.locks.ReentrantReadWriteLock}.
*
* NOTE: This test will fail. It was written to demonstrate a fault in the class
* under test.
*
* @author alasdair.mackintosh@gmail.com (Alasdair Mackintosh)
*/
public class UserCache {
private final UserDb db;
private final String username;
private String avatar;
private RenderingContext context;
volatile boolean cacheValid;
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
UserCache(UserDb db, String username, RenderingContext context) {
this.db = db;
this.username = username;
this.context = context;
updateCache();
cacheValid = true;
}
void updateCache() {
avatar = db.getAvatar(username);
}
public void drawUserAvatar() {
rwl.readLock().lock();
if (!cacheValid) {
// upgrade lock manually
rwl.readLock().unlock(); // must unlock first to obtain writelock
rwl.writeLock().lock();
if (!cacheValid) { // recheck
updateCache();
cacheValid = true;
}
// downgrade lock
rwl.readLock().lock(); // reacquire read without giving up write lock
rwl.writeLock().unlock(); // unlock write, still hold read
}
context.draw(avatar);
rwl.readLock().unlock();
}
/** Invoked when the user changes. Marks the cache as dirty. */
public void updateUser() {
db.update(username);
cacheValid = false;
}
}