Skip to content

Commit 7f40487

Browse files
committed
Merge pull request Beerkay#1 from IOAyman/master
deleted some of the unnecessary code blocks, code is styled and by IOAyman
2 parents 240fbb2 + b15dc75 commit 7f40487

File tree

25 files changed

+288
-240
lines changed

25 files changed

+288
-240
lines changed

JavaMultiThreadingCodes/src/CallableAndFuture_13/CallableTester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Runnable Introduced in Java 1.0 Callable<T> Introduced in Java 1.5 as part of
1919
* java.util.concurrent library
2020
*
21-
* Runnable cannot be parameterized Callable is a parameterized type whose type
21+
* Runnable cannot be parametrized Callable is a parametrized type whose type
2222
* parameter indicates the return type of its run method Classes implementing
2323
*
2424
* Runnable needs to implement run() method, classes implementing Callable needs

JavaMultiThreadingCodes/src/CountDownLatch_6/App.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,34 @@
55
import java.util.concurrent.Executors;
66

77
/**
8-
* CountDownLatch Java class to synchronize your threads’ activities.
9-
*
8+
* {@link java.util.concurrent.CountDownLatch} Java class to synchronize your threads’ activities.
9+
* <br><br>
1010
* Source:
11-
* (http://stackoverflow.com/questions/17827022/what-is-countdown-latch-in-java-multithreading)
11+
* <em>http://stackoverflow.com/questions/17827022/what-is-countdown-latch-in-java-multithreading</em><br>
12+
*
1213
* Any thread, usually main thread of application, which calls
13-
* CountDownLatch.await() will wait until count reaches zero or its interrupted
14+
* {@link java.util.concurrent.CountDownLatch#await()} will wait until count reaches zero or its interrupted
1415
* by another thread. All other thread are required to do count down by calling
15-
* CountDownLatch.countDown() once they are completed or ready.
16-
*
16+
* {@link java.util.concurrent.CountDownLatch#countDown()} once they are completed or ready.
17+
* <br>
1718
* As soon as count reaches zero, Thread awaiting starts running. One of the
18-
* disadvantage of CountDownLatch is that its not reusable once count reaches to
19-
* zero you can not use CountDownLatch any more.
20-
*
21-
* Use CountDownLatch when one thread like main thread, require to wait for one
22-
* or more thread to complete, before it can start processing.
23-
*
24-
* Classical example of using CountDownLatch in Java is any server side core
25-
* Java application which uses services architecture, where multiple services
19+
* disadvantage of {@link java.util.concurrent.CountDownLatch} is that it's
20+
* not reusable once the count reaches to
21+
* zero you can not use {@link java.util.concurrent.CountDownLatch} any more.
22+
* <br><br>
23+
* Use {@link java.util.concurrent.CountDownLatch} when one thread, like main
24+
* thread, require to wait for one or more threads to complete, before it can
25+
* start processing.
26+
* <br><br>
27+
* Classical example of using {@link java.util.concurrent.CountDownLatch} in
28+
* Java is any server side core Java application which uses services
29+
* architecture, where multiple services
2630
* are provided by multiple threads and application can not start processing
2731
* until all services have started successfully.
28-
*
29-
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/
32+
* <br><br>
33+
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br>
3034
* also freely available at
31-
* https://www.udemy.com/java-multithreading/?couponCode=FREE
35+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
3236
*
3337
* @author Z.B. Celik <[email protected]>
3438
*/
@@ -45,10 +49,7 @@ public void run() {
4549

4650
try {
4751
Thread.sleep(3000);
48-
} catch (InterruptedException e) {
49-
// TODO Auto-generated catch block
50-
e.printStackTrace();
51-
}
52+
} catch (InterruptedException ignored) {}
5253
latch.countDown();
5354
}
5455
}
@@ -61,8 +62,10 @@ public static void main(String[] args) {
6162
for (int i = 0; i < 3; i++) {
6263
executor.submit(new Processor(latch));
6364
}
65+
executor.shutdown();
66+
6467
try {
65-
// Application’s main thread waits, till other service threads which are
68+
// Application’s main thread waits, till other service threads which are
6669
// as an example responsible for starting framework services have completed started all services.
6770
latch.await();
6871
} catch (InterruptedException e) {

JavaMultiThreadingCodes/src/Deadlock_11/Runner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void acquireLocks(Lock firstLock, Lock secondLock) throws InterruptedExc
5353
}
5454
}
5555

56-
//firstThreaad runs
56+
//firstThread runs
5757
public void firstThread() throws InterruptedException {
5858
Random random = new Random();
5959
for (int i = 0; i < 10000; i++) {

JavaMultiThreadingCodes/src/Deadlock_11/SimpleDeadLock.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*/
1111
public class SimpleDeadLock {
1212

13-
public static Object lock1 = new Object();
14-
public static Object lock2 = new Object();
13+
public static final Object lock1 = new Object();
14+
public static final Object lock2 = new Object();
1515
private int index;
1616

1717
public static void main(String[] a) {
@@ -28,7 +28,7 @@ public void run() {
2828
System.out.println("Thread 1: Holding lock 1...");
2929
try {
3030
Thread.sleep(10);
31-
} catch (InterruptedException e) {
31+
} catch (InterruptedException ignored) {
3232
}
3333
System.out.println("Thread 1: Waiting for lock 2...");
3434
synchronized (lock2) {
@@ -45,7 +45,7 @@ public void run() {
4545
System.out.println("Thread 2: Holding lock 2...");
4646
try {
4747
Thread.sleep(10);
48-
} catch (InterruptedException e) {
48+
} catch (InterruptedException ignored) {
4949
}
5050
System.out.println("Thread 2: Waiting for lock 1...");
5151
synchronized (lock1) {

JavaMultiThreadingCodes/src/InterruptingThreads14/App.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public static void main(String[] args) throws InterruptedException {
3131

3232
System.out.println("Starting.");
3333

34-
ExecutorService executer = Executors.newCachedThreadPool();
34+
ExecutorService executor = Executors.newCachedThreadPool();
3535

36-
Future<?> fu = executer.submit(new Callable<Void>() {
36+
Future<?> fu = executor.submit(new Callable<Void>() {
3737

3838
@Override
3939
public Void call() throws Exception {
@@ -49,11 +49,11 @@ public Void call() throws Exception {
4949
}
5050
});
5151

52-
executer.shutdown();
52+
executor.shutdown();
5353
Thread.sleep(500);
54-
executer.shutdownNow();
54+
executor.shutdownNow();
5555

56-
executer.awaitTermination(1, TimeUnit.DAYS);
56+
executor.awaitTermination(1, TimeUnit.DAYS);
5757
System.out.println("Finished.");
5858
}
5959

JavaMultiThreadingCodes/src/JoiningAndSynchronizeThreads_3/Worker.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import java.util.logging.Logger;
55

66
/**
7-
* synchronized ("only let one thread in here at a time".) and join ("wait until
7+
* {@code synchronized} ("only let one thread in here at a time".) and {@code join} ("wait until
88
* thread on which join has called finished") keyword.
9-
*
10-
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/
9+
* <br><br>
10+
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br>
1111
* also freely available at
12-
* https://www.udemy.com/java-multithreading/?couponCode=FREE
12+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
1313
*
1414
* @author Z.B. Celik <[email protected]>
1515
*/
@@ -70,10 +70,7 @@ public void run() {
7070
try {
7171
thread1.join();
7272
thread2.join();
73-
} catch (InterruptedException e) {
74-
// TODO Auto-generated catch block
75-
e.printStackTrace();
76-
}
73+
} catch (InterruptedException ignored) {}
7774
System.out.println("Count is: " + count);
7875
}
7976
}

JavaMultiThreadingCodes/src/LockObjects_4/App.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package LockObjects_4;
22

33
/**
4-
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/
4+
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br>
55
* also freely available at
6-
* https://www.udemy.com/java-multithreading/?couponCode=FREE
6+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
77
*
88
* @author Z.B. Celik <[email protected]>
99
*/

JavaMultiThreadingCodes/src/LockObjects_4/Worker.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public class Worker {
2121

2222
private Random random = new Random();
2323

24-
private Object lock1 = new Object();
25-
private Object lock2 = new Object();
24+
private final Object lock1 = new Object();
25+
private final Object lock2 = new Object();
2626

27-
private List<Integer> list1 = new ArrayList<Integer>();
28-
private List<Integer> list2 = new ArrayList<Integer>();
27+
private List<Integer> list1 = new ArrayList<>();
28+
private List<Integer> list2 = new ArrayList<>();
2929

3030
public void stageOne() {
3131

JavaMultiThreadingCodes/src/LockObjects_4/WorkerMethodsSynchronized.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class WorkerMethodsSynchronized {
2121

2222
private Random random = new Random();
2323

24-
private List<Integer> list1 = new ArrayList<Integer>();
25-
private List<Integer> list2 = new ArrayList<Integer>();
24+
private List<Integer> list1 = new ArrayList<>();
25+
private List<Integer> list2 = new ArrayList<>();
2626

2727
/**
2828
* synchronized, methods use different data (list1 list2) so by synchronized
@@ -78,10 +78,7 @@ public void run() {
7878
try {
7979
t1.join();
8080
t2.join();
81-
} catch (InterruptedException e) {
82-
// TODO Auto-generated catch block
83-
e.printStackTrace();
84-
}
81+
} catch (InterruptedException ignored) {}
8582

8683
long end = System.currentTimeMillis();
8784

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package LowLevelProducerConsumer_9;
22

33
/**
4-
* How to implement the producer-consumer pattern using "low level" techniques;
4+
* How to implement the Producer-Consumer pattern using "low level" techniques;
55
* namely, wait, notify and synchronized. This isn't the best way to implement a
6-
* producer-consumer pattern in Java (see tutorial 7 use of BlockingQueue for
6+
* Producer-Consumer pattern in Java
7+
* (see tutorial 7 use of {@link java.util.concurrent.BlockingQueue} for
78
* the best way); but this tutorial will help you to understand how to use wait
89
* and notify.
9-
*
10-
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/
10+
* <br><br>
11+
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br>
1112
* also freely available at
12-
* https://www.udemy.com/java-multithreading/?couponCode=FREE
13+
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
1314
*
1415
* @author Z.B. Celik <[email protected]>
1516
*/
@@ -22,9 +23,7 @@ public static void main(String[] args) throws InterruptedException {
2223
public void run() {
2324
try {
2425
processor.produce();
25-
} catch (InterruptedException e) {
26-
e.printStackTrace();
27-
}
26+
} catch (InterruptedException ignored) {}
2827
}
2928
});
3029

@@ -33,14 +32,18 @@ public void run() {
3332
public void run() {
3433
try {
3534
processor.consume();
36-
} catch (InterruptedException e) {
37-
e.printStackTrace();
38-
}
35+
} catch (InterruptedException ignored) {}
3936
}
4037
});
38+
4139
t1.start();
4240
t2.start();
43-
t1.join();
44-
t2.join();
41+
// t1.join();
42+
// t2.join();
43+
44+
// Pause for 30 seconds and force quitting the app (because we're
45+
// looping infinitely)
46+
Thread.sleep(30000);
47+
System.exit(0);
4548
}
4649
}

0 commit comments

Comments
 (0)