8000 update format of ConnectionLimiter · fishercoder1534/RandomJava@8332304 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8332304

Browse files
update format of ConnectionLimiter
1 parent 9ad20f8 commit 8332304

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

src/main/java/semaphore/ConnectionLimiter.java

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,66 @@
55
/**
66
* This is a small program to demo how semaphore can create trouble for us.
77
* One rule of thumb is:
8-
* Always release what you acquire!
9-
* Copied from online.
10-
*
8+
* Always release what you acquire!
9+
* Copied from the Internet.
1110
*/
1211
//this is a bad public class name, it doesn't apply to
1312
//what this class really do, it's a name from a different class that I copied earlier.
1413
public class ConnectionLimiter {
1514

16-
private static class DoubleResourceGrabber implements Runnable{
17-
18-
private Semaphore first;
19-
private Semaphore second;
20-
21-
public DoubleResourceGrabber(Semaphore s1, Semaphore s2){
22-
first = s1;
23-
second = s2;
24-
}
25-
26-
@Override
27-
public void run() {
28-
Thread t = Thread.currentThread();
29-
30-
try {
31-
first.acquire();
32-
System.out.println(t.getName() + " acquired " + first);
33-
15+
private static class DoubleResourceGrabber implements Runnable {
16+
17+
private Semaphore first;
18+
private Semaphore second;
19+
20+
public DoubleResourceGrabber(Semaphore s1, Semaphore s2) {
21+
first = s1;
22+
second = s2;
23+
}
24+
25+
@Override
26+
public void run() {
27+
Thread t = Thread.currentThread();
28+
29+
try {
30+
first.acquire();
31+
System.out.println(t.getName() + " acquired " + first);
32+
3433
// Thread.sleep(20);//to demo a deadlock
35-
34+
3635
// second.acquire();
3736
// System.out.println(t.getName() + " acquired " + second);
38-
37+
3938
// second.release();
4039
// System.out.println(t.getName() + " released " + second);
41-
42-
first.release();
43-
System.out.println(t.getName() + " released " + first);
44-
45-
} catch (InterruptedException e) {
46-
e.printStackTrace();
47-
}
48-
}
49-
}
50-
51-
/**
52-
* @param args
53-
* @throws InterruptedException
54-
*/
55-
public static void main(String[] args) throws InterruptedException {
56-
Semaphore s1 = new Semaphore(1);//give it only 1 permit
57-
Semaphore s2 = new Semaphore(1);//give it only 1 permit as well
58-
Thread t1 = new Thread(new DoubleResourceGrabber(s1, s2));
59-
//now reverse them, here comes the trouble
60-
Thread t2 = new Thread(new DoubleResourceGrabber(s2, s1));
61-
62-
t1.start();
63-
t2.start();
64-
65-
t1.join();
66-
t2.join();
67-
System.out.println("We got lucky!");
68-
69-
}
40+
41+
first.release();
42+
System.out.println(t.getName() + " released " + first);
43+
44+
} catch (InterruptedException e) {
45+
e.printStackTrace();
46+
}
47+
}
48+
}
49+
50+
/**
51+
* @param args
52+
* @throws InterruptedException
53+
*/
54+
public static void main(String[] args) throws InterruptedException {
55+
Semaphore s1 = new Semaphore(1);//give it only 1 permit
56+
Semaphore s2 = new Semaphore(1);//give it only 1 permit as well
57+
Thread t1 = new Thread(new DoubleResourceGrabber(s1, s2));
58+
//now reverse them, here comes the trouble
59+
Thread t2 = new Thread(new DoubleResourceGrabber(s2, s1));
60+
61+
t1.start();
62+
t2.start();
63+
64+
t1.join();
65+
t2.join();
66+
System.out.println("We got lucky!");
67+
68+
}
7069

7170
}

0 commit comments

Comments
 (0)
0