Skip to content

Commit 9192686

Browse files
committed
JavaDoc optimized
1 parent ddc9c62 commit 9192686

File tree

5 files changed

+163
-63
lines changed

5 files changed

+163
-63
lines changed

JavaMultiThreadingCodes/src/Deadlock_11/Runner.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44
import java.util.concurrent.locks.Lock;
55
import java.util.concurrent.locks.ReentrantLock;
66

7-
/**
8-
* <a href="https://wikipedia.org/wiki/Deadlock">Deadlock</a>
9-
* <br><br>
10-
* Codes with minor comments are from
11-
* <a href="http://www.caveofprogramming.com/youtube/">
12-
* <em>http://www.caveofprogramming.com/youtube/</em>
13-
* </a>
14-
* <br>
15-
* also freely available at
16-
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
17-
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
18-
* </a>
19-
*
20-
* @author Z.B. Celik <[email protected]>
21-
*/
22-
public class Runner {
7+
/**
8+
* <a href="https://wikipedia.org/wiki/Deadlock">Deadlock</a>
9+
* <br><br>
10+
* Codes with minor comments are from
11+
* <a href="http://www.caveofprogramming.com/youtube/">
12+
* <em>http://www.caveofprogramming.com/youtube/</em>
13+
* </a>
14+
* <br>
15+
* also freely available at
16+
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
17+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
18+
* </a>
19+
*
20+
* @author Z.B. Celik <[email protected]>
21+
*/
22+
@SuppressWarnings("InfiniteLoopStatement")
23+
public class Runner {
2324

2425
private Account acc1 = new Account();
2526
private Account acc2 = new Account();
@@ -44,15 +45,9 @@ private void acquireLocks(Lock firstLock, Lock secondLock) throws InterruptedExc
4445
gotFirstLock = firstLock.tryLock();
4546
gotSecondLock = secondLock.tryLock();
4647
} finally {
47-
if (gotFirstLock && gotSecondLock) {
48-
return;
49-
}
50-
if (gotFirstLock) {
51-
firstLock.unlock();
52-
}
53-
if (gotSecondLock) {
54-
secondLock.unlock();
55-
}
48+
if (gotFirstLock && gotSecondLock) return;
49+
else if (gotFirstLock) firstLock.unlock();
50+
else if (gotSecondLock) secondLock.unlock();
5651
}
5752
// Locks not acquired
5853
Thread.sleep(1);

JavaMultiThreadingCodes/src/Deadlock_11/SimpleDeadLock.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
/**
44
* Source: http://www.herongyang.com/Java/Deadlock-What-Is-Deadlock.html
5-
* Deadlock is a programming situation where two or more threads are blocked
5+
* <p>
6+
* <a href="https://wikipedia.org/wiki/Deadlock">Deadlock</a>
7+
* is a programming situation where two or more threads are blocked
68
* forever, this situation arises with at least two threads and two or more
79
* resources.
810
*
@@ -28,8 +30,7 @@ public void run() {
2830
System.out.println("Thread 1: Holding lock 1...");
2931
try {
3032
Thread.sleep(10);
31-
} catch (InterruptedException ignored) {
32-
}
33+
} catch (InterruptedException ignored) {}
3334
System.out.println("Thread 1: Waiting for lock 2...");
3435
synchronized (lock2) {
3536
System.out.println("Thread 2: Holding lock 1 & 2...");
@@ -45,8 +46,7 @@ public void run() {
4546
System.out.println("Thread 2: Holding lock 2...");
4647
try {
4748
Thread.sleep(10);
48-
} catch (InterruptedException ignored) {
49-
}
49+
} catch (InterruptedException ignored) {}
5050
System.out.println("Thread 2: Waiting for lock 1...");
5151
synchronized (lock1) {
5252
System.out.println("Thread 2: Holding lock 2 & 1...");

JavaMultiThreadingCodes/src/Semaphores_12/App.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,61 @@
55
import java.util.concurrent.TimeUnit;
66

77
/**
8-
* Semaphores are mainly used to limit the number of simultaneous threads that
8+
* {@link java.util.concurrent.Semaphore}s
9+
* are mainly used to limit the number of simultaneous threads that
910
* can access a resources, but you can also use them to implement deadlock
1011
* recovery systems since a semaphore with one permit is basically a lock that
11-
* you can unlock from other threads.
12-
*
12+
* can unlock from other threads.
13+
* <br>
1314
* Source:
15+
* <a href="http://stackoverflow.com/questions/771347/what-is-mutex-and-semaphore-in-java-what-is-the-main-difference">
1416
* http://stackoverflow.com/questions/771347/what-is-mutex-and-semaphore-in-java-what-is-the-main-difference
15-
*
16-
* Mutex is basically mutual exclusion. Only one thread can acquire the resource
17-
* at once. When one thread acquires the resource, no other thread is allowed to
18-
* acquire the resource until the thread owning the resource releases. All
19-
* threads waiting for acquiring resource would be blocked.
20-
*
17+
* </a>
18+
* <p>
19+
* Mutex (or a semaphore initialized to 1; meaning there's only one resource)
20+
* is basically a mutual exclusion; Only one thread can acquire the resource
21+
* at once, and all other threads trying to acquire the resource are blocked
22+
* until the thread owning the resource releases.
23+
* <p>
2124
* Semaphore is used to control the number of threads executing. There will be
2225
* fixed set of resources. The resource count will gets decremented every time
2326
* when a thread owns the same. When the semaphore count reaches 0 then no other
2427
* threads are allowed to acquire the resource. The threads get blocked till
2528
* other threads owning resource releases.
26-
*
29+
* </p>
30+
* <p>
2731
* In short, the main difference is how many threads are allowed to acquire the
28-
* resource at once ?
29-
*
32+
* resource at once.
33+
* TODO -- go a little more in depth explaining that
3034
* Mutex --its ONE. Semaphore -- its DEFINED_COUNT, ( as many as semaphore
3135
* count)
32-
*
33-
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/
36+
* </p>
37+
* <br><br>
38+
* Codes with minor comments are from
39+
* <a href="http://www.caveofprogramming.com/youtube/">
40+
* <em>http://www.caveofprogramming.com/youtube/</em>
41+
* </a>
42+
* <br>
3443
* also freely available at
35-
* https://www.udemy.com/java-multithreading/?couponCode=FREE
44+
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
45+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
46+
* </a>
3647
*
3748
* @author Z.B. Celik <[email protected]>
3849
*/
3950
public class App {
4051

4152
public static void main(String[] args) throws Exception {
4253
ExecutorService executor = Executors.newCachedThreadPool();
43-
for (int i = 0; i < 13; i++) { //200 hundred times will be called
54+
55+
for (int i = 0; i < 20; i++) { //200 hundred times will be called
4456
executor.submit(new Runnable() {
4557
public void run() {
46-
Connection.getInstance().connect();
58+
Connectionn.getInstance().connect();
4759
}
4860
});
4961
}
62+
5063
executor.shutdown();
5164
executor.awaitTermination(1, TimeUnit.DAYS);
5265
}

JavaMultiThreadingCodes/src/Semaphores_12/Connection.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,38 @@
44

55
/**
66
* Semaphores
7-
*
8-
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/
7+
* <br><br>
8+
* Codes with minor comments are from
9+
* <a href="http://www.caveofprogramming.com/youtube/">
10+
* <em>http://www.caveofprogramming.com/youtube/</em>
11+
* </a>
12+
* <br>
913
* also freely available at
10-
* https://www.udemy.com/java-multithreading/?couponCode=FREE
14+
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
15+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
16+
* </a>
1117
*
1218
* @author Z.B. Celik <[email protected]>
1319
*/
1420
public class Connection {
1521

1622
private static Connection instance = new Connection();
17-
//limit connections to 10
18-
//true means whichever the thread call the acquire first gets it first,
19-
//in queue placed first to obtain the permit.
23+
/*
24+
limit connections to 10
25+
true means whichever thread gets first in the waiting pool (queue)
26+
waiting to acquire a resource, is first to obtain the permit.
27+
28+
Note that I called it a pool!
29+
The reason is when you say "Queue", you're saying that things are
30+
scheduled to be FIFO (First In First Out) .. which is not always the case
31+
here!
32+
When you initialize the semaphore with Fairness, by setting its second
33+
argument to true, it will treat the waiting threads like FIFO.
34+
But,
35+
it doesn't have to be that way if you don't set on the fairness. the JVM
36+
may schedule the waiting threads in some other manner that it sees best
37+
(See the Java specifications for that).
38+
*/
2039
private Semaphore sem = new Semaphore(10, true);
2140
private int connections = 0;
2241

@@ -29,16 +48,18 @@ public static Connection getInstance() {
2948

3049
public void connect() {
3150
try {
32-
sem.acquire(); // get permit decrease the sem value, if 0 wait for release
33-
} catch (InterruptedException e1) {
34-
e1.printStackTrace();
35-
}
36-
try {
51+
52+
// get permit decrease the sem value, if 0 wait for release
53+
sem.acquire();
54+
3755
//if doConnect throws and exception is still releases the permit
38-
//so we use a separate method here to increase the connections count.
56+
//so we use a separate method here to increase the connections count
3957
doConnect();
58+
59+
} catch (InterruptedException ignored) {
4060
} finally {
41-
sem.release();// release permit, increase the sem value and activate waiting thread
61+
//release permit, increase the sem value and activate waiting thread
62+
sem.release();
4263
}
4364
}
4465

@@ -51,9 +72,7 @@ public void doConnect() {
5172
//do your job
5273
System.out.println("Working on connections " + Thread.currentThread().getName());
5374
Thread.sleep(2000);
54-
} catch (InterruptedException e) {
55-
e.printStackTrace();
56-
}
75+
} catch (InterruptedException ignored) {}
5776
//when exit doConnect method decrement number of connections
5877
synchronized (this) {//atomic
5978
connections--;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package Semaphores_12;
2+
3+
import java.util.concurrent.Semaphore;
4+
5+
/**
6+
* Semaphores
7+
* <br><br>
8+
* Codes with minor comments are from
9+
* <a href="http://www.caveofprogramming.com/youtube/">
10+
* <em>http://www.caveofprogramming.com/youtube/</em>
11+
* </a>
12+
* <br>
13+
* also freely available at
14+
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
15+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
16+
* </a>
17+
*
18+
* @author Z.B. Celik <[email protected]>
19+
*/
20+
public class Connectionn {
21+
22+
private static Connectionn instance = new Connectionn();
23+
/*
24+
limit connections to 10
25+
true means whichever thread gets first in the waiting pool (queue)
26+
waiting to acquire a resource, is first to obtain the permit.
27+
28+
Note that I called it a pool!
29+
The reason is when you say "Queue", you're saying that things are
30+
scheduled to be FIFO (First In First Out) .. which is not always the case
31+
here!
32+
When you initialize the semaphore with Fairness, by setting its second
33+
argument to true, it will treat the waiting threads like FIFO.
34+
But,
35+
it doesn't have to be that way if you don't set on the fairness. the JVM
36+
may schedule the waiting threads in some other manner that it sees best
37+
(See the Java specifications for that).
38+
*/
39+
private Semaphore sem = new Semaphore(10, true);
40+
41+
private Connectionn() {
42+
}
43+
44+
public static Connectionn getInstance() {
45+
return instance;
46+
}
47+
48+
public void connect() {
49+
try {
50+
51+
// get permit decrease the sem value, if 0 wait for release
52+
sem.acquire();
53+
54+
System.out.printf("%s:: Current connections (max 10 allowed): %d\n",
55+
Thread.currentThread().getName(),
56+
sem.availablePermits());
57+
58+
//do your job
59+
System.out.printf("%s:: WORKING...\n",
60+
Thread.currentThread().getName());
61+
Thread.sleep(2000);
62+
63+
System.out.printf("%s:: Connection released. Permits Left = %d\n",
64+
Thread.currentThread().getName(),
65+
sem.availablePermits());
66+
67+
} catch (InterruptedException ignored) {
68+
} finally {
69+
//release permit, increase the sem value and activate waiting thread
70+
sem.release();
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)