forked from hansiming/JavaProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutexEvenGenerator.java
More file actions
37 lines (27 loc) · 696 Bytes
/
MutexEvenGenerator.java
File metadata and controls
37 lines (27 loc) · 696 Bytes
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
package com.csdhsm.concurrent;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MutexEvenGenerator extends IntGenerator {
private int currentEvenValue = 0;
private Lock lock = new ReentrantLock();
@Override
public int next() {
lock.lock();
try {
++currentEvenValue;
++currentEvenValue;
Thread.sleep(1000);
System.out.println("1");
return currentEvenValue;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//lock.unlock();
}
return currentEvenValue;
}
public static void main(String[] args) {
EvenChecker.test(new MutexEvenGenerator());
}
}