*/
-import java.util.Random;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
-
+@SuppressWarnings("InfiniteLoopStatement")
public class Runner {
private Account acc1 = new Account();
@@ -38,15 +45,9 @@ private void acquireLocks(Lock firstLock, Lock secondLock) throws InterruptedExc
gotFirstLock = firstLock.tryLock();
gotSecondLock = secondLock.tryLock();
} finally {
- if (gotFirstLock && gotSecondLock) {
- return;
- }
- if (gotFirstLock) {
- firstLock.unlock();
- }
- if (gotSecondLock) {
- secondLock.unlock();
- }
+ if (gotFirstLock && gotSecondLock) return;
+ else if (gotFirstLock) firstLock.unlock();
+ else if (gotSecondLock) secondLock.unlock();
}
// Locks not acquired
Thread.sleep(1);
diff --git a/JavaMultiThreadingCodes/src/Deadlock_11/SimpleDeadLock.java b/JavaMultiThreadingCodes/src/Deadlock_11/SimpleDeadLock.java
index 10a9594..9ad7f65 100644
--- a/JavaMultiThreadingCodes/src/Deadlock_11/SimpleDeadLock.java
+++ b/JavaMultiThreadingCodes/src/Deadlock_11/SimpleDeadLock.java
@@ -2,7 +2,9 @@
/**
* Source: http://www.herongyang.com/Java/Deadlock-What-Is-Deadlock.html
- * Deadlock is a programming situation where two or more threads are blocked
+ *
+ * Deadlock
+ * is a programming situation where two or more threads are blocked
* forever, this situation arises with at least two threads and two or more
* resources.
*
@@ -28,8 +30,7 @@ public void run() {
System.out.println("Thread 1: Holding lock 1...");
try {
Thread.sleep(10);
- } catch (InterruptedException ignored) {
- }
+ } catch (InterruptedException ignored) {}
System.out.println("Thread 1: Waiting for lock 2...");
synchronized (lock2) {
System.out.println("Thread 2: Holding lock 1 & 2...");
@@ -45,8 +46,7 @@ public void run() {
System.out.println("Thread 2: Holding lock 2...");
try {
Thread.sleep(10);
- } catch (InterruptedException ignored) {
- }
+ } catch (InterruptedException ignored) {}
System.out.println("Thread 2: Waiting for lock 1...");
synchronized (lock1) {
System.out.println("Thread 2: Holding lock 2 & 1...");
diff --git a/JavaMultiThreadingCodes/src/InterruptingThreads14/App.java b/JavaMultiThreadingCodes/src/InterruptingThreads14/App.java
index 165a460..a843f1e 100644
--- a/JavaMultiThreadingCodes/src/InterruptingThreads14/App.java
+++ b/JavaMultiThreadingCodes/src/InterruptingThreads14/App.java
@@ -1,27 +1,31 @@
package InterruptingThreads14;
-import java.util.Random;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.*;
/**
- * how to interrupt running threads in Java using the built-in thread
- * interruption mechanism.
- *
- * Source:http://www.javamex.com/tutorials/threads/thread_interruption.shtml
- * Incidentally, it is important not to confuse thread interruption with either
+ * How to interrupt running threads in Java using the built-in thread
+ * interruption mechanism.
+ *
+ * Source:
+ *
+ * http://www.javamex.com/tutorials/threads/thread_interruption.shtml
+ *
+ * Incidentally, it is important NOT to confuse thread interruption with either
* software interrupts (where the CPU automatically interrupts the current
* instruction flow in order to call a registered piece of code periodically— as
* in fact happens to drive the thread scheduler) and hardware interrupts (where
* the CPU automatically performs a similar task in response to some hardware
* signal).
- *
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ *
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
@@ -37,20 +41,30 @@ public static void main(String[] args) throws InterruptedException {
@Override
public Void call() throws Exception {
- Random ran = new Random();
+
for (int i = 0; i < 1E8; i++) {
if (Thread.currentThread().isInterrupted()) {
- System.out.println("Interrupted!");
+ System.out.printf("Interrupted at %d !!!", i);
break;
}
- Math.sin(ran.nextDouble());
}
+
return null;
}
});
executor.shutdown();
Thread.sleep(500);
+
+ /*
+ in this example, there are different ways you can interrupt a thread
+ execution.
+ */
+
+ //JavaDoc: http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html#cancel-boolean-
+// fu.cancel(true);
+
+ //JavaDoc: http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html#shutdownNow--
executor.shutdownNow();
executor.awaitTermination(1, TimeUnit.DAYS);
diff --git a/JavaMultiThreadingCodes/src/JoiningAndSynchronizeThreads_3/Worker.java b/JavaMultiThreadingCodes/src/JoiningAndSynchronizeThreads_3/Worker.java
index a419f3c..e89df8a 100644
--- a/JavaMultiThreadingCodes/src/JoiningAndSynchronizeThreads_3/Worker.java
+++ b/JavaMultiThreadingCodes/src/JoiningAndSynchronizeThreads_3/Worker.java
@@ -7,9 +7,15 @@
* {@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/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/LockObjects_4/App.java b/JavaMultiThreadingCodes/src/LockObjects_4/App.java
index e98d96f..c7941fb 100644
--- a/JavaMultiThreadingCodes/src/LockObjects_4/App.java
+++ b/JavaMultiThreadingCodes/src/LockObjects_4/App.java
@@ -1,9 +1,15 @@
package LockObjects_4;
/**
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/LockObjects_4/Worker.java b/JavaMultiThreadingCodes/src/LockObjects_4/Worker.java
index fd5d7a7..70ad012 100644
--- a/JavaMultiThreadingCodes/src/LockObjects_4/Worker.java
+++ b/JavaMultiThreadingCodes/src/LockObjects_4/Worker.java
@@ -10,10 +10,16 @@
* making the method synchronized or making an object inside the method
* synchronized, By defining two different locks we say that one thread may
* execute the stageOne while other executes stageTwo.
- *
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ *
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/LockObjects_4/WorkerMethodsSynchronized.java b/JavaMultiThreadingCodes/src/LockObjects_4/WorkerMethodsSynchronized.java
index d122f57..c01b492 100644
--- a/JavaMultiThreadingCodes/src/LockObjects_4/WorkerMethodsSynchronized.java
+++ b/JavaMultiThreadingCodes/src/LockObjects_4/WorkerMethodsSynchronized.java
@@ -10,10 +10,16 @@
* making the method synchronized or making "different" objects inside the
* method synchronized, By defining two different locks we say that one thread
* may execute the stageOne while other executes stageTwo.
- *
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ *
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/LowLevelProducerConsumer_9/App.java b/JavaMultiThreadingCodes/src/LowLevelProducerConsumer_9/App.java
index 3e62cf3..aabbfd3 100644
--- a/JavaMultiThreadingCodes/src/LowLevelProducerConsumer_9/App.java
+++ b/JavaMultiThreadingCodes/src/LowLevelProducerConsumer_9/App.java
@@ -8,9 +8,15 @@
* 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/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/LowLevelProducerConsumer_9/Processor.java b/JavaMultiThreadingCodes/src/LowLevelProducerConsumer_9/Processor.java
index e9f5a1d..e061c1d 100644
--- a/JavaMultiThreadingCodes/src/LowLevelProducerConsumer_9/Processor.java
+++ b/JavaMultiThreadingCodes/src/LowLevelProducerConsumer_9/Processor.java
@@ -1,15 +1,21 @@
package LowLevelProducerConsumer_9;
+import java.util.LinkedList;
+import java.util.Random;
+
/**
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
+ *
* https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
-import java.util.LinkedList;
-import java.util.Random;
-
@SuppressWarnings("InfiniteLoopStatement")
public class Processor {
diff --git a/JavaMultiThreadingCodes/src/ProducerConsumer_7/App.java b/JavaMultiThreadingCodes/src/ProducerConsumer_7/App.java
index 1f62f46..b064da4 100644
--- a/JavaMultiThreadingCodes/src/ProducerConsumer_7/App.java
+++ b/JavaMultiThreadingCodes/src/ProducerConsumer_7/App.java
@@ -8,9 +8,15 @@
* data items and adding them to a shared data store of some kind while one or
* more other threads process those items, removing them from the data store.
*
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/ReentrantLocks_10/App.java b/JavaMultiThreadingCodes/src/ReentrantLocks_10/App.java
index 3e17bff..845fbe7 100644
--- a/JavaMultiThreadingCodes/src/ReentrantLocks_10/App.java
+++ b/JavaMultiThreadingCodes/src/ReentrantLocks_10/App.java
@@ -54,9 +54,15 @@
* http://guruzon.com/1/concurrency/explicit-lock-locking/difference-between-synchronized-and-reentrantlock-in-java
*
*
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/ReentrantLocks_10/Runner.java b/JavaMultiThreadingCodes/src/ReentrantLocks_10/Runner.java
index 4159ea3..b97b4af 100644
--- a/JavaMultiThreadingCodes/src/ReentrantLocks_10/Runner.java
+++ b/JavaMultiThreadingCodes/src/ReentrantLocks_10/Runner.java
@@ -35,9 +35,15 @@
* that are similar to
* {@link Object#notify()} and {@link Object#notifyAll()} methods.
*
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/Semaphores_12/App.java b/JavaMultiThreadingCodes/src/Semaphores_12/App.java
index 6c1790b..154da74 100644
--- a/JavaMultiThreadingCodes/src/Semaphores_12/App.java
+++ b/JavaMultiThreadingCodes/src/Semaphores_12/App.java
@@ -5,34 +5,45 @@
import java.util.concurrent.TimeUnit;
/**
- * Semaphores are mainly used to limit the number of simultaneous threads that
+ * {@link java.util.concurrent.Semaphore}s
+ * are mainly used to limit the number of simultaneous threads that
* can access a resources, but you can also use them to implement deadlock
* recovery systems since a semaphore with one permit is basically a lock that
- * you can unlock from other threads.
- *
+ * can unlock from other threads.
+ *
* Source:
+ *
* http://stackoverflow.com/questions/771347/what-is-mutex-and-semaphore-in-java-what-is-the-main-difference
- *
- * Mutex is basically mutual exclusion. Only one thread can acquire the resource
- * at once. When one thread acquires the resource, no other thread is allowed to
- * acquire the resource until the thread owning the resource releases. All
- * threads waiting for acquiring resource would be blocked.
- *
+ *
+ *
+ * Mutex (or a semaphore initialized to 1; meaning there's only one resource)
+ * is basically a mutual exclusion; Only one thread can acquire the resource
+ * at once, and all other threads trying to acquire the resource are blocked
+ * until the thread owning the resource releases.
+ *
* Semaphore is used to control the number of threads executing. There will be
* fixed set of resources. The resource count will gets decremented every time
* when a thread owns the same. When the semaphore count reaches 0 then no other
* threads are allowed to acquire the resource. The threads get blocked till
* other threads owning resource releases.
- *
+ *
+ *
* In short, the main difference is how many threads are allowed to acquire the
- * resource at once ?
- *
+ * resource at once.
+ * TODO -- go a little more in depth explaining that
* Mutex --its ONE. Semaphore -- its DEFINED_COUNT, ( as many as semaphore
* count)
- *
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ *
+ *
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
@@ -40,13 +51,15 @@ public class App {
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newCachedThreadPool();
- for (int i = 0; i < 13; i++) { //200 hundred times will be called
+
+ for (int i = 0; i < 20; i++) { //200 hundred times will be called
executor.submit(new Runnable() {
public void run() {
- Connection.getInstance().connect();
+ Connectionn.getInstance().connect();
}
});
}
+
executor.shutdown();
executor.awaitTermination(1, TimeUnit.DAYS);
}
diff --git a/JavaMultiThreadingCodes/src/Semaphores_12/Connection.java b/JavaMultiThreadingCodes/src/Semaphores_12/Connection.java
index b4c77d7..88836e2 100644
--- a/JavaMultiThreadingCodes/src/Semaphores_12/Connection.java
+++ b/JavaMultiThreadingCodes/src/Semaphores_12/Connection.java
@@ -4,19 +4,38 @@
/**
* Semaphores
- *
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ *
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
public class Connection {
private static Connection instance = new Connection();
- //limit connections to 10
- //true means whichever the thread call the acquire first gets it first,
- //in queue placed first to obtain the permit.
+/*
+ limit connections to 10
+ true means whichever thread gets first in the waiting pool (queue)
+ waiting to acquire a resource, is first to obtain the permit.
+
+ Note that I called it a pool!
+ The reason is when you say "Queue", you're saying that things are
+ scheduled to be FIFO (First In First Out) .. which is not always the case
+ here!
+ When you initialize the semaphore with Fairness, by setting its second
+ argument to true, it will treat the waiting threads like FIFO.
+ But,
+ it doesn't have to be that way if you don't set on the fairness. the JVM
+ may schedule the waiting threads in some other manner that it sees best
+ (See the Java specifications for that).
+*/
private Semaphore sem = new Semaphore(10, true);
private int connections = 0;
@@ -29,16 +48,18 @@ public static Connection getInstance() {
public void connect() {
try {
- sem.acquire(); // get permit decrease the sem value, if 0 wait for release
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
- try {
+
+ // get permit decrease the sem value, if 0 wait for release
+ sem.acquire();
+
//if doConnect throws and exception is still releases the permit
- //so we use a separate method here to increase the connections count.
+ //so we use a separate method here to increase the connections count
doConnect();
+
+ } catch (InterruptedException ignored) {
} finally {
- sem.release();// release permit, increase the sem value and activate waiting thread
+ //release permit, increase the sem value and activate waiting thread
+ sem.release();
}
}
@@ -51,9 +72,7 @@ public void doConnect() {
//do your job
System.out.println("Working on connections " + Thread.currentThread().getName());
Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
+ } catch (InterruptedException ignored) {}
//when exit doConnect method decrement number of connections
synchronized (this) {//atomic
connections--;
diff --git a/JavaMultiThreadingCodes/src/Semaphores_12/Connectionn.java b/JavaMultiThreadingCodes/src/Semaphores_12/Connectionn.java
new file mode 100644
index 0000000..0171cc7
--- /dev/null
+++ b/JavaMultiThreadingCodes/src/Semaphores_12/Connectionn.java
@@ -0,0 +1,73 @@
+package Semaphores_12;
+
+import java.util.concurrent.Semaphore;
+
+/**
+ * Semaphores
+ *
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
+ * also freely available at
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ *
+ * @author Z.B. Celik
+ */
+public class Connectionn {
+
+ private static Connectionn instance = new Connectionn();
+ /*
+ limit connections to 10
+ true means whichever thread gets first in the waiting pool (queue)
+ waiting to acquire a resource, is first to obtain the permit.
+
+ Note that I called it a pool!
+ The reason is when you say "Queue", you're saying that things are
+ scheduled to be FIFO (First In First Out) .. which is not always the case
+ here!
+ When you initialize the semaphore with Fairness, by setting its second
+ argument to true, it will treat the waiting threads like FIFO.
+ But,
+ it doesn't have to be that way if you don't set on the fairness. the JVM
+ may schedule the waiting threads in some other manner that it sees best
+ (See the Java specifications for that).
+ */
+ private Semaphore sem = new Semaphore(10, true);
+
+ private Connectionn() {
+ }
+
+ public static Connectionn getInstance() {
+ return instance;
+ }
+
+ public void connect() {
+ try {
+
+ // get permit decrease the sem value, if 0 wait for release
+ sem.acquire();
+
+ System.out.printf("%s:: Current connections (max 10 allowed): %d\n",
+ Thread.currentThread().getName(),
+ sem.availablePermits());
+
+ //do your job
+ System.out.printf("%s:: WORKING...\n",
+ Thread.currentThread().getName());
+ Thread.sleep(2000);
+
+ System.out.printf("%s:: Connection released. Permits Left = %d\n",
+ Thread.currentThread().getName(),
+ sem.availablePermits());
+
+ } catch (InterruptedException ignored) {
+ } finally {
+ //release permit, increase the sem value and activate waiting thread
+ sem.release();
+ }
+ }
+}
diff --git a/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationAnonymous.java b/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationAnonymous.java
index fd61eb2..8e11f90 100644
--- a/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationAnonymous.java
+++ b/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationAnonymous.java
@@ -3,9 +3,15 @@
/**
* Starting threads using the Thread constructor with anonymous classes
*
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationExtends.java b/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationExtends.java
index e966135..68d346a 100644
--- a/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationExtends.java
+++ b/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationExtends.java
@@ -3,8 +3,15 @@
/**
* Starting Threads with extends
*
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
- * also freely available at https://www.udemy.com/java-multithreading/?couponCode=FREE
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
+ * also freely available at
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationRunnable.java b/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationRunnable.java
index 768cfc3..a7561e3 100644
--- a/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationRunnable.java
+++ b/JavaMultiThreadingCodes/src/StartingThreads_1/ApplicationRunnable.java
@@ -3,8 +3,15 @@
/**
* Starting Threads using Runnable Interface
*
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
- * also freely available at: https://www.udemy.com/java-multithreading/?couponCode=FREE
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
+ * also freely available at
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/ThreadPools_5/App.java b/JavaMultiThreadingCodes/src/ThreadPools_5/App.java
index 649ea6c..3cc0c33 100644
--- a/JavaMultiThreadingCodes/src/ThreadPools_5/App.java
+++ b/JavaMultiThreadingCodes/src/ThreadPools_5/App.java
@@ -3,9 +3,15 @@
/**
* ThreadPool ("number of workers in a factory")
*
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/ThreadPools_5/WorkerThreadPool.java b/JavaMultiThreadingCodes/src/ThreadPools_5/WorkerThreadPool.java
index 5da9ee5..6251a4f 100644
--- a/JavaMultiThreadingCodes/src/ThreadPools_5/WorkerThreadPool.java
+++ b/JavaMultiThreadingCodes/src/ThreadPools_5/WorkerThreadPool.java
@@ -10,9 +10,15 @@
/**
* This is the implementation of {@link LockObjects_4.Worker} with threadPool
*
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/VolatileKeyword_2/App.java b/JavaMultiThreadingCodes/src/VolatileKeyword_2/App.java
index c133425..027ac29 100644
--- a/JavaMultiThreadingCodes/src/VolatileKeyword_2/App.java
+++ b/JavaMultiThreadingCodes/src/VolatileKeyword_2/App.java
@@ -4,9 +4,15 @@
* Volatile Keyword, “… the volatile modifier guarantees that any thread that
* reads a field will see the most recently written value.” - Josh Bloch
*
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/WaitAndNotify_8/App.java b/JavaMultiThreadingCodes/src/WaitAndNotify_8/App.java
index f45c252..c6e2b07 100644
--- a/JavaMultiThreadingCodes/src/WaitAndNotify_8/App.java
+++ b/JavaMultiThreadingCodes/src/WaitAndNotify_8/App.java
@@ -6,10 +6,16 @@
* 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
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/JavaMultiThreadingCodes/src/WaitAndNotify_8/BlockingQueueApp.java b/JavaMultiThreadingCodes/src/WaitAndNotify_8/BlockingQueueApp.java
index 843f7cc..99f76ca 100644
--- a/JavaMultiThreadingCodes/src/WaitAndNotify_8/BlockingQueueApp.java
+++ b/JavaMultiThreadingCodes/src/WaitAndNotify_8/BlockingQueueApp.java
@@ -47,8 +47,8 @@
* safely take out the first connection. This checking and removing will be
* atomic because we have the lock on the list.
*
- * @author Z.B. Celik
*
+ * @author Z.B. Celik
*/
//For the other version of the implementation please check
// LowLevelProducerConsumer_9.App
@@ -86,12 +86,12 @@ public T take() throws InterruptedException {
try {
while (queue.isEmpty()) {
System.out.println("queue is empty, cannot take");
- notEmpty.await();//releases lock
+ notEmpty.await(); //releases lock
}
T item = queue.remove();
System.out.println("Removed to the queue " + item);
- notFull.signal();//call waiting thread on same object
+ notFull.signal(); //calls waiting thread on same object
return item;
} finally {
lock.unlock();
diff --git a/JavaMultiThreadingCodes/src/WaitAndNotify_8/Processor.java b/JavaMultiThreadingCodes/src/WaitAndNotify_8/Processor.java
index 18e965d..21171c1 100644
--- a/JavaMultiThreadingCodes/src/WaitAndNotify_8/Processor.java
+++ b/JavaMultiThreadingCodes/src/WaitAndNotify_8/Processor.java
@@ -23,9 +23,15 @@
* {@link Object#notify()} wakes up the first thread that called wait() on
* the same object.
*
- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
+ * Codes with minor comments are from
+ *
+ * http://www.caveofprogramming.com/youtube/
+ *
+ *
* also freely available at
- * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
+ * https://www.udemy.com/java-multithreading/?couponCode=FREE
+ *
*
* @author Z.B. Celik
*/
diff --git a/README.md b/README.md
index 873dc08..09aaeb1 100644
--- a/README.md
+++ b/README.md
@@ -1,32 +1,30 @@
-Java Multithreading
-=============================================================
+#Java Multithreading
This repository will contain all the codes for the ultimate Java multithreading course by John Purcell
See the [Video Tutorials](https://www.udemy.com/java-multithreading/)
for more information.
-Contributor
-----------
-[Z.B. Celik] (http://www.linkedin.com/in/berkaycelik)
+##Contributors
+[Z.B. Celik] (http://www.linkedin.com/in/berkaycelik)
+[@IOAyman] (https://twitter.com/IOAyman)
-Java Multithreading Topics:
--------------
+##Java Multithreading Topics:
Codes with minor comments are from http://www.caveofprogramming.com/youtube/ also freely available at https://www.udemy.com/java-multithreading/?couponCode=FREE
- 1- Java Multithreading: Starting Threads
-- 2- Java Multithreading: Volatile – Basic Thread Communication
-- 3- Java Multithreading: Synchronized
-- 4- Java Multithreading: Lock Objects
-- 5- Java Multithreading: Thread Pools
-- 6- Java Multithreading: Countdown Latches
-- 7- Java Multithreading: Producer-Consumer
-- 8- Java Multithreading: Wait and Notify
-- 9- Java Multithreading: Low-Level Producer-Consumer
-- 10- Java Multithreading: Re-entrant Locks
-- 11- Java Multithreading: Deadlock
-- 12- Java Multithreading: Semaphores
-- 13- Java Multithreading: Callable and Future
-- 14- Java Multithreading: Interrupting Threads
+- 2- Java Multithreading: Volatile – Basic Thread Communication
+- 3- Java Multithreading: Synchronized
+- 4- Java Multithreading: Lock Objects
+- 5- Java Multithreading: Thread Pools
+- 6- Java Multithreading: Countdown Latches
+- 7- Java Multithreading: Producer-Consumer
+- 8- Java Multithreading: Wait and Notify
+- 9- Java Multithreading: Low-Level Producer-Consumer
+- 10- Java Multithreading: Re-entrant Locks
+- 11- Java Multithreading: Deadlock
+- 12- Java Multithreading: Semaphores
+- 13- Java Multithreading: Callable and Future
+- 14- Java Multithreading: Interrupting Threads