Skip to content

Commit b15dc75

Browse files
committed
JavaDoc optimized for packages 10
1 parent 52628e5 commit b15dc75

File tree

3 files changed

+89
-53
lines changed

3 files changed

+89
-53
lines changed

JavaMultiThreadingCodes/src/ReentrantLocks_10/App.java

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,62 @@
11
package ReentrantLocks_10;
22

33
/**
4-
* the ReentrantLock class in Java as an alternative to synchronized code
5-
* blocks. ReentrantLocks let you do all the stuff that you can do with
6-
* synchronized, wait and notify, plus some more stuff besides that may come in
4+
* the {@link java.util.concurrent.locks.ReentrantLock} class in Java as an
5+
* alternative to synchronized code blocks.
6+
* <br>
7+
* {@link java.util.concurrent.locks.ReentrantLock}s let you do all the
8+
* stuff that you can do with {@code synchronized}, {@link Object#wait()} and
9+
* {@link Object#notify()}, plus some more stuff. Besides that may come in
710
* handy from time to time.
8-
*
9-
* Source:
11+
* <br><br>
12+
* Source:<em>
1013
* http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html
11-
*
12-
* ReentrantLock Extended capabilities include:
13-
*
14-
* The ability to have more than one condition variable per monitor. Monitors
15-
* that use the synchronized keyword can only have one. This means reentrant
16-
* locks support more than one wait()/notify() queue. The ability to make the
17-
* lock "fair". "[fair] locks favor granting access to the longest-waiting
14+
* </em>
15+
* <br><br>
16+
* {@link java.util.concurrent.locks.ReentrantLock} Extended capabilities
17+
* include:
18+
* <br>
19+
* <ul>
20+
* <li>
21+
* The ability to have more than one {@link java.util.concurrent.locks.Condition}
22+
* variable per monitor.
23+
* </li>
24+
* <li>Monitors that use the synchronized keyword can only have one. This means
25+
* {@link java.util.concurrent.locks.ReentrantLock}s support more than one
26+
* {@link Object#wait()}/{@link Object#notify()} queue.
27+
* </li>
28+
* <li>
29+
* The ability to make the lock "fair".
30+
* <em>
31+
* "[fair] locks favor granting access to the longest-waiting
1832
* thread. Otherwise this lock does not guarantee any particular access order."
19-
* Synchronized blocks are unfair. The ability to check if the lock is being
20-
* held. The ability to get the list of threads waiting on the lock.
21-
*
22-
* The disadvantages of reentrant locks are:
23-
*
24-
* Need to add import statement. Need to wrap lock acquisitions in a try/finally
25-
* block. This makes it more ugly than the synchronized keyword. The
26-
* synchronized keyword can be put in method definitions which avoids the need
27-
* for a block which reduces nesting. For more complete comparison of
28-
* reentrantLocks and synchronized see
33+
* </em>
34+
* </li>
35+
* <li>Synchronized blocks are unfair.</li>
36+
* <li>The ability to check if the lock is being
37+
* held.</li>
38+
* <li>The ability to get the list of threads waiting on the lock.</li>
39+
* </ul>
40+
* <br><br>
41+
* The disadvantages of {@link java.util.concurrent.locks.ReentrantLock}s are:
42+
* <br>
43+
* <ul>
44+
* <li>Need to add import statement.</li>
45+
* <li>Need to wrap lock acquisitions in a try/finally block. This makes it more
46+
* ugly than the synchronized keyword.</li>
47+
* <li>The synchronized keyword can be put in method definitions which avoids
48+
* the need for a block which reduces nesting.</li>
49+
* </ul>
50+
* <br><br>
51+
* For more complete comparison of
52+
* {@link java.util.concurrent.locks.ReentrantLock}s and {@code synchronized}
53+
* see:<em>
2954
* http://guruzon.com/1/concurrency/explicit-lock-locking/difference-between-synchronized-and-reentrantlock-in-java
30-
*
31-
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/
55+
* </em>
56+
* <br><br>
57+
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br>
3258
* also freely available at
33-
* https://www.udemy.com/java-multithreading/?couponCode=FREE
59+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
3460
*
3561
* @author Z.B. Celik <[email protected]>
3662
*/
@@ -42,8 +68,7 @@ public static void main(String[] args) throws Exception {
4268
public void run() {
4369
try {
4470
runner.firstThread();
45-
} catch (InterruptedException e) {
46-
e.printStackTrace();
71+
} catch (InterruptedException ignored) {
4772
}
4873
}
4974
});
@@ -52,8 +77,7 @@ public void run() {
5277
public void run() {
5378
try {
5479
runner.secondThread();
55-
} catch (InterruptedException e) {
56-
e.printStackTrace();
80+
} catch (InterruptedException ignored) {
5781
}
5882
}
5983
});

JavaMultiThreadingCodes/src/ReentrantLocks_10/Runner.java

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,46 @@
11
package ReentrantLocks_10;
22

3+
import java.util.Scanner;
4+
import java.util.concurrent.locks.Condition;
5+
import java.util.concurrent.locks.Lock;
6+
import java.util.concurrent.locks.ReentrantLock;
37
/**
4-
* Source:http://www.journaldev.com/2377/java-lock-example-and-concurrency-lock-vs-synchronized
5-
*
6-
* Lock: This is the base interface for Lock API. It provides all the features
8+
* Source:<em>
9+
* http://www.journaldev.com/2377/java-lock-example-and-concurrency-lock-vs-synchronized</em>
10+
* <p>
11+
* {@link java.util.concurrent.locks.Lock}:
12+
* This is the base interface for Lock API. It provides all the features
713
* of synchronized keyword with additional ways to create different Conditions
814
* for locking, providing timeout for thread to wait for lock. Some of the
9-
* important methods are lock() to acquire the lock, unlock() to release the
10-
* lock, tryLock() to wait for lock for a certain period of time, newCondition()
15+
* important methods are {@link java.util.concurrent.locks.Lock#lock()} to
16+
* acquire the lock, {@link java.util.concurrent.locks.Lock#unlock()} to release
17+
* the lock, {@link java.util.concurrent.locks.Lock#tryLock()} to wait for lock
18+
* for a certain period of time,
19+
* {@link java.util.concurrent.locks.Lock#newCondition()}
1120
* to create the Condition etc.
12-
*
13-
* ReentrantLock: This is the most widely used implementation class of Lock
21+
* <p>
22+
* {@link java.util.concurrent.locks.ReentrantLock}:
23+
* This is the most widely used implementation class of Lock
1424
* interface. This class implements the Lock interface in similar way as
1525
* synchronized keyword. (see App.java for more)
16-
*
17-
* Condition: Condition objects are similar to Object wait-notify model with
26+
* <p>
27+
* {@link java.util.concurrent.locks.Condition}:
28+
* Condition objects are similar to Object wait-notify model with
1829
* additional feature to create different sets of wait. A Condition object is
19-
* always created by Lock object. Some of the important methods are await() that
20-
* is similar to wait() and signal(), signalAll() that is similar to notify()
21-
* and notifyAll() methods.
22-
*
23-
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/
30+
* always created by Lock object. Some of the important methods are
31+
* {@link java.util.concurrent.locks.Condition#await()}
32+
* that is similar to {@link Object#wait()}.
33+
* {@link java.util.concurrent.locks.Condition#signal()} and
34+
* {@link java.util.concurrent.locks.Condition#signalAll()}
35+
* that are similar to
36+
* {@link Object#notify()} and {@link Object#notifyAll()} methods.
37+
* <br><br>
38+
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br>
2439
* also freely available at
25-
* https://www.udemy.com/java-multithreading/?couponCode=FREE
40+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
2641
*
2742
* @author Z.B. Celik <[email protected]>
2843
*/
29-
import java.util.Scanner;
30-
import java.util.concurrent.locks.Condition;
31-
import java.util.concurrent.locks.Lock;
32-
import java.util.concurrent.locks.ReentrantLock;
33-
3444
public class Runner {
3545

3646
private int count = 0;
@@ -65,7 +75,8 @@ public void secondThread() throws InterruptedException {
6575
try {
6676
increment();
6777
} finally {
68-
lock.unlock();//should be written to unlock Thread whenever signal() is called
78+
//should be written to unlock Thread whenever signal() is called
79+
lock.unlock();
6980
}
7081
}
7182

JavaMultiThreadingCodes/src/WaitAndNotify_8/BlockingQueueApp.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
* @author Z.B. Celik <[email protected]>
5151
*
5252
*/
53-
//For the other version of the implementation please check LowLevelProducerConsumer_9.App
53+
//For the other version of the implementation please check
54+
// LowLevelProducerConsumer_9.App
5455
class BlockingQueue<T> {
5556

5657
private Queue<T> queue = new LinkedList<>();
@@ -69,12 +70,12 @@ public void put(T element) throws InterruptedException {
6970
try {
7071
while (queue.size() == capacity) {
7172
System.out.println("queue is full cannot put");
72-
notFull.await();//releases lock
73+
notFull.await(); //releases lock
7374
}
7475

7576
queue.add(element);
7677
System.out.println("Added to the queue " + element);
77-
notEmpty.signal();//call waiting thread on same object
78+
notEmpty.signal(); //calls waiting thread on the same object
7879
} finally {
7980
lock.unlock();
8081
}

0 commit comments

Comments
 (0)