forked from Beerkay/JavaMultiThreading
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Beerkay#1 from IOAyman/master
deleted some of the unnecessary code blocks, code is styled and by IOAyman
- Loading branch information
Showing
25 changed files
with
288 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,30 +5,34 @@ | |
import java.util.concurrent.Executors; | ||
|
||
/** | ||
* CountDownLatch Java class to synchronize your threads’ activities. | ||
* | ||
* {@link java.util.concurrent.CountDownLatch} Java class to synchronize your threads’ activities. | ||
* <br><br> | ||
* Source: | ||
* (http://stackoverflow.com/questions/17827022/what-is-countdown-latch-in-java-multithreading) | ||
* <em>http://stackoverflow.com/questions/17827022/what-is-countdown-latch-in-java-multithreading</em><br> | ||
* | ||
* Any thread, usually main thread of application, which calls | ||
* CountDownLatch.await() will wait until count reaches zero or its interrupted | ||
* {@link java.util.concurrent.CountDownLatch#await()} will wait until count reaches zero or its interrupted | ||
* by another thread. All other thread are required to do count down by calling | ||
* CountDownLatch.countDown() once they are completed or ready. | ||
* | ||
* {@link java.util.concurrent.CountDownLatch#countDown()} once they are completed or ready. | ||
* <br> | ||
* As soon as count reaches zero, Thread awaiting starts running. One of the | ||
* disadvantage of CountDownLatch is that its not reusable once count reaches to | ||
* zero you can not use CountDownLatch any more. | ||
* | ||
* Use CountDownLatch when one thread like main thread, require to wait for one | ||
* or more thread to complete, before it can start processing. | ||
* | ||
* Classical example of using CountDownLatch in Java is any server side core | ||
* Java application which uses services architecture, where multiple services | ||
* disadvantage of {@link java.util.concurrent.CountDownLatch} is that it's | ||
* not reusable once the count reaches to | ||
* zero you can not use {@link java.util.concurrent.CountDownLatch} any more. | ||
* <br><br> | ||
* Use {@link java.util.concurrent.CountDownLatch} when one thread, like main | ||
* thread, require to wait for one or more threads to complete, before it can | ||
* start processing. | ||
* <br><br> | ||
* Classical example of using {@link java.util.concurrent.CountDownLatch} in | ||
* Java is any server side core Java application which uses services | ||
* architecture, where multiple services | ||
* are provided by multiple threads and application can not start processing | ||
* until all services have started successfully. | ||
* | ||
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/ | ||
* <br><br> | ||
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br> | ||
* also freely available at | ||
* https://www.udemy.com/java-multithreading/?couponCode=FREE | ||
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em> | ||
* | ||
* @author Z.B. Celik <[email protected]> | ||
*/ | ||
|
@@ -45,10 +49,7 @@ public void run() { | |
|
||
try { | ||
Thread.sleep(3000); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} catch (InterruptedException ignored) {} | ||
latch.countDown(); | ||
} | ||
} | ||
|
@@ -61,8 +62,10 @@ public static void main(String[] args) { | |
for (int i = 0; i < 3; i++) { | ||
executor.submit(new Processor(latch)); | ||
} | ||
executor.shutdown(); | ||
|
||
try { | ||
// Application’s main thread waits, till other service threads which are | ||
// Application’s main thread waits, till other service threads which are | ||
// as an example responsible for starting framework services have completed started all services. | ||
latch.await(); | ||
} catch (InterruptedException e) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,12 @@ | |
import java.util.logging.Logger; | ||
|
||
/** | ||
* synchronized ("only let one thread in here at a time".) and join ("wait until | ||
* {@code synchronized} ("only let one thread in here at a time".) and {@code join} ("wait until | ||
* thread on which join has called finished") keyword. | ||
* | ||
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/ | ||
* <br><br> | ||
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br> | ||
* also freely available at | ||
* https://www.udemy.com/java-multithreading/?couponCode=FREE | ||
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em> | ||
* | ||
* @author Z.B. Celik <[email protected]> | ||
*/ | ||
|
@@ -70,10 +70,7 @@ public void run() { | |
try { | ||
thread1.join(); | ||
thread2.join(); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} catch (InterruptedException ignored) {} | ||
System.out.println("Count is: " + count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package LockObjects_4; | ||
|
||
/** | ||
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/ | ||
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br> | ||
* also freely available at | ||
* https://www.udemy.com/java-multithreading/?couponCode=FREE | ||
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em> | ||
* | ||
* @author Z.B. Celik <[email protected]> | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
package LowLevelProducerConsumer_9; | ||
|
||
/** | ||
* How to implement the producer-consumer pattern using "low level" techniques; | ||
* How to implement the Producer-Consumer pattern using "low level" techniques; | ||
* namely, wait, notify and synchronized. This isn't the best way to implement a | ||
* producer-consumer pattern in Java (see tutorial 7 use of BlockingQueue for | ||
* Producer-Consumer pattern in Java | ||
* (see tutorial 7 use of {@link java.util.concurrent.BlockingQueue} for | ||
* the best way); but this tutorial will help you to understand how to use wait | ||
* and notify. | ||
* | ||
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/ | ||
* <br><br> | ||
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br> | ||
* also freely available at | ||
* https://www.udemy.com/java-multithreading/?couponCode=FREE | ||
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em> | ||
* | ||
* @author Z.B. Celik <[email protected]> | ||
*/ | ||
|
@@ -22,9 +23,7 @@ public static void main(String[] args) throws InterruptedException { | |
public void run() { | ||
try { | ||
processor.produce(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} catch (InterruptedException ignored) {} | ||
} | ||
}); | ||
|
||
|
@@ -33,14 +32,18 @@ public void run() { | |
public void run() { | ||
try { | ||
processor.consume(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} catch (InterruptedException ignored) {} | ||
} | ||
}); | ||
|
||
t1.start(); | ||
t2.start(); | ||
t1.join(); | ||
t2.join(); | ||
// t1.join(); | ||
// t2.join(); | ||
|
||
// Pause for 30 seconds and force quitting the app (because we're | ||
// looping infinitely) | ||
Thread.sleep(30000); | ||
System.exit(0); | ||
} | ||
} |
9 changes: 5 additions & 4 deletions
9
JavaMultiThreadingCodes/src/LowLevelProducerConsumer_9/Processor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
package LowLevelProducerConsumer_9; | ||
|
||
/** | ||
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/ | ||
* Codes with minor comments are from <em>http://www.caveofprogramming.com/youtube/</em><br> | ||
* also freely available at | ||
* https://www.udemy.com/java-multithreading/?couponCode=FREE | ||
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em> | ||
* | ||
* @author Z.B. Celik <[email protected]> | ||
*/ | ||
import java.util.LinkedList; | ||
import java.util.Random; | ||
|
||
@SuppressWarnings("InfiniteLoopStatement") | ||
public class Processor { | ||
|
||
private LinkedList<Integer> list = new LinkedList<>(); | ||
private final int LIMIT = 10; | ||
private Object lock = new Object(); | ||
private final Object lock = new Object(); | ||
|
||
public void produce() throws InterruptedException { | ||
int value = 0; | ||
while (true) { | ||
synchronized (lock) { | ||
//whenever the threade is notofied starts again from the loop | ||
//whenever the thread is notified starts again from the loop | ||
while (list.size() == LIMIT) { | ||
lock.wait();// wait() is also true | ||
} | ||
|
Oops, something went wrong.