Skip to content

Commit

Permalink
Merge pull request Beerkay#1 from IOAyman/master
Browse files Browse the repository at this point in the history
 deleted some of the unnecessary code blocks, code is styled and  by IOAyman
  • Loading branch information
Beerkay committed Jan 11, 2015
2 parents 240fbb2 + b15dc75 commit 7f40487
Show file tree
Hide file tree
Showing 25 changed files with 288 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Runnable Introduced in Java 1.0 Callable<T> Introduced in Java 1.5 as part of
* java.util.concurrent library
*
* Runnable cannot be parameterized Callable is a parameterized type whose type
* Runnable cannot be parametrized Callable is a parametrized type whose type
* parameter indicates the return type of its run method Classes implementing
*
* Runnable needs to implement run() method, classes implementing Callable needs
Expand Down
47 changes: 25 additions & 22 deletions JavaMultiThreadingCodes/src/CountDownLatch_6/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
*/
Expand All @@ -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();
}
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion JavaMultiThreadingCodes/src/Deadlock_11/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void acquireLocks(Lock firstLock, Lock secondLock) throws InterruptedExc
}
}

//firstThreaad runs
//firstThread runs
public void firstThread() throws InterruptedException {
Random random = new Random();
for (int i = 0; i < 10000; i++) {
Expand Down
8 changes: 4 additions & 4 deletions JavaMultiThreadingCodes/src/Deadlock_11/SimpleDeadLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
public class SimpleDeadLock {

public static Object lock1 = new Object();
public static Object lock2 = new Object();
public static final Object lock1 = new Object();
public static final Object lock2 = new Object();
private int index;

public static void main(String[] a) {
Expand All @@ -28,7 +28,7 @@ public void run() {
System.out.println("Thread 1: Holding lock 1...");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
} catch (InterruptedException ignored) {
}
System.out.println("Thread 1: Waiting for lock 2...");
synchronized (lock2) {
Expand All @@ -45,7 +45,7 @@ public void run() {
System.out.println("Thread 2: Holding lock 2...");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
} catch (InterruptedException ignored) {
}
System.out.println("Thread 2: Waiting for lock 1...");
synchronized (lock1) {
Expand Down
10 changes: 5 additions & 5 deletions JavaMultiThreadingCodes/src/InterruptingThreads14/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public static void main(String[] args) throws InterruptedException {

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

ExecutorService executer = Executors.newCachedThreadPool();
ExecutorService executor = Executors.newCachedThreadPool();

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

@Override
public Void call() throws Exception {
Expand All @@ -49,11 +49,11 @@ public Void call() throws Exception {
}
});

executer.shutdown();
executor.shutdown();
Thread.sleep(500);
executer.shutdownNow();
executor.shutdownNow();

executer.awaitTermination(1, TimeUnit.DAYS);
executor.awaitTermination(1, TimeUnit.DAYS);
System.out.println("Finished.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
*/
Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions JavaMultiThreadingCodes/src/LockObjects_4/App.java
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]>
*/
Expand Down
8 changes: 4 additions & 4 deletions JavaMultiThreadingCodes/src/LockObjects_4/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public class Worker {

private Random random = new Random();

private Object lock1 = new Object();
private Object lock2 = new Object();
private final Object lock1 = new Object();
private final Object lock2 = new Object();

private List<Integer> list1 = new ArrayList<Integer>();
private List<Integer> list2 = new ArrayList<Integer>();
private List<Integer> list1 = new ArrayList<>();
private List<Integer> list2 = new ArrayList<>();

public void stageOne() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class WorkerMethodsSynchronized {

private Random random = new Random();

private List<Integer> list1 = new ArrayList<Integer>();
private List<Integer> list2 = new ArrayList<Integer>();
private List<Integer> list1 = new ArrayList<>();
private List<Integer> list2 = new ArrayList<>();

/**
* synchronized, methods use different data (list1 list2) so by synchronized
Expand Down Expand Up @@ -78,10 +78,7 @@ public void run() {
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InterruptedException ignored) {}

long end = System.currentTimeMillis();

Expand Down
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,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
}
Expand Down
Loading

0 comments on commit 7f40487

Please sign in to comment.