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.
JavaDoc optimized for packages 7.8.9
Suppress inspection Warnings about infinite loop statements
- Loading branch information
Showing
6 changed files
with
56 additions
and
45 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
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); | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
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,15 +1,16 @@ | ||
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<>(); | ||
|
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,14 +1,15 @@ | ||
package WaitAndNotify_8; | ||
|
||
/** | ||
* wait and notify in Java; low-level multithreading methods of the Object class | ||
* {@link Object#wait()} and {@link Object#notify()} in Java; low-level | ||
* multi-threading methods of the {@link java.lang.Object} class | ||
* that allow you to have one or more threads sleeping, only to be woken up by | ||
* other threads at the right moment. Extremely useful for avoiding those | ||
* processor-consuming "polling loops". | ||
* | ||
* 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]> | ||
*/ | ||
|
@@ -21,9 +22,7 @@ public static void main(String[] args) throws InterruptedException { | |
public void run() { | ||
try { | ||
processor.produce(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} catch (InterruptedException ignored) {} | ||
} | ||
}); | ||
|
||
|
@@ -32,11 +31,10 @@ public void run() { | |
public void run() { | ||
try { | ||
processor.consume(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} catch (InterruptedException ignored) {} | ||
} | ||
}); | ||
|
||
t1.start(); | ||
t2.start(); | ||
t1.join(); | ||
|
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 |
---|---|---|
|
@@ -3,32 +3,42 @@ | |
import java.util.Scanner; | ||
|
||
/** | ||
* Some background knowledge | ||
* Source:http://www.programcreek.com/2009/02/notify-and-wait-example/ | ||
* synchronized keyword is used for exclusive accessing. To make a method | ||
* synchronized, simply add the synchronized keyword to its declaration. Then no | ||
* two invocations of synchronized methods on the same object can interleave | ||
* with each other. Synchronized statements must specify the object that | ||
* provides the intrinsic lock. When synchronized(this) is used, you have to | ||
* avoid to synchronizing invocations of other objects' methods. wait() tells | ||
* Some background knowledge<br> | ||
* Source: <em>http://www.programcreek.com/2009/02/notify-and-wait-example/</em> | ||
* <br><br> | ||
* {@code synchronized} keyword is used for exclusive accessing. To make a | ||
* method {@code synchronized}, simply add the {@code synchronized} keyword to its | ||
* declaration.<be> | ||
* Then no two invocations of synchronized methods on the same object can | ||
* interleave with each other. | ||
* <br> | ||
* Synchronized statements must specify the object that | ||
* provides the intrinsic lock. When {@code synchronized(this)} is used, you | ||
* have to avoid to synchronizing invocations of other objects' methods. | ||
* <br> | ||
* {@link Object#wait()} tells | ||
* the calling thread to give up the lock and go to sleep (not polling) until | ||
* some other thread enters the same lock and calls notify(). notify() wakes up | ||
* the first thread that called wait() on the same object. | ||
* | ||
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/ | ||
* some other thread enters the same lock and calls {@link Object#notify()}. | ||
* <br> | ||
* {@link Object#notify()} wakes up the first thread that called wait() on | ||
* the same object. | ||
* <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]> | ||
*/ | ||
public class Processor { | ||
|
||
/** | ||
/* | ||
* public synchronized void getSomething(){ this.hello = "hello World"; } | ||
* public void getSomething(){ synchronized(this){ this.hello = "hello | ||
* World"; } } | ||
* two code blocks by specification, functionally identical. | ||
*/ | ||
|
||
|
||
public void produce() throws InterruptedException { | ||
synchronized (this) { | ||
System.out.println("Producer thread running ...."); | ||
|