-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample6.java
64 lines (48 loc) · 1.88 KB
/
Example6.java
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
package applications.threading;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import static java.util.concurrent.Executors.newFixedThreadPool;
/**
* Category: Threading
* ID: Example6
* Description: Illustrates the Lock interface
* Taken From:
*
* Details:
* A critical region can be created b y using locks. Each object is associated with a lock in the form of a monitor which
* ensures that only one thread at a time can execute the code in a critical region.
* Java provides the Lock interface for modeling locks. Several classes implement it.
* The principle is simple. Before accessing the critical region the thread must obtain the lock. Once exiting it, it must release the lock.
* Since code can always throw exceptions, we must place the unlocking into the finally block.
*/
public class Example6 {
public class Task implements Runnable
{
private Lock lock;
public Task(Lock lock){
this.lock = lock;
}
@Override
public void run(){
try {
lock.lock();
System.out.println("HelloWorld from thread: " + Thread.currentThread().getName()+ " with id: "+Thread.currentThread().getId());
}
finally {
lock.unlock();
}
}
}
public static void main(String[] args){
Example6 example6 = new Example6();
Lock lock = new ReentrantLock();
ExecutorService executorService = newFixedThreadPool(3);
executorService.submit(example6.new Task(lock));
executorService.submit(example6.new Task(lock));
executorService.submit(example6.new Task(lock));
executorService.submit(example6.new Task(lock));
executorService.submit(example6.new Task(lock));
executorService.shutdown();
}
}