Skip to content

Commit

Permalink
JavaDoc optimized for packages 7.8.9
Browse files Browse the repository at this point in the history
Suppress inspection Warnings about infinite loop statements
  • Loading branch information
IOAyman committed Jan 11, 2015
1 parent b65b85e commit 52628e5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 45 deletions.
29 changes: 16 additions & 13 deletions JavaMultiThreadingCodes/src/LowLevelProducerConsumer_9/App.java
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]>
*/
Expand All @@ -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) {}
}
});

Expand All @@ -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);
}
}
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<>();
Expand Down
4 changes: 3 additions & 1 deletion JavaMultiThreadingCodes/src/ProducerConsumer_7/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

@SuppressWarnings("InfiniteLoopStatement")
public class App {

/**
Expand Down Expand Up @@ -57,7 +58,8 @@ public void run() {
// t1.join();
// t2.join();

// Pause for 30 seconds and force quitting the app (because we're looping infinitely)
// Pause for 30 seconds and force quitting the app (because we're
// looping infinitely)
Thread.sleep(30000);
System.exit(0);
}
Expand Down
16 changes: 7 additions & 9 deletions JavaMultiThreadingCodes/src/WaitAndNotify_8/App.java
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]>
*/
Expand All @@ -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) {}
}
});

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public T take() throws InterruptedException {
}
}

@SuppressWarnings("InfiniteLoopStatement")
public class BlockingQueueApp {

public static void main(String[] args) throws InterruptedException {
Expand All @@ -109,9 +110,7 @@ public void run() {
while (true) {
blockingQueue.put(random.nextInt(10));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (InterruptedException ignored) {}
}
});

Expand All @@ -126,9 +125,7 @@ public void run() {
while (true) {
blockingQueue.take();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (InterruptedException ignored) {}
}
});
t1.start();
Expand Down
38 changes: 24 additions & 14 deletions JavaMultiThreadingCodes/src/WaitAndNotify_8/Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ....");
Expand Down

0 comments on commit 52628e5

Please sign in to comment.