Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
JavaDoc optimized
Added myself to README.md
  • Loading branch information
IOAyman committed Jan 24, 2015
commit b83db5e3d199ead351efa679271ec275a0d9ffa6
21 changes: 9 additions & 12 deletions JavaMultiThreadingCodes/src/CallableAndFuture_13/App2.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package CallableAndFuture_13;

import java.util.ArrayList;
import java.util.concurrent.*;

/**
* Understanding Callable
*
* @author Z.B. Celik <[email protected]>
*/
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

class MyCallable implements Callable<Integer> {

Integer value;
int value;

public MyCallable(Integer i) {
public MyCallable(int i) {
this.value = i;
}

Expand All @@ -38,8 +33,8 @@ public class App2 {
public static void main(String[] args) throws InterruptedException {
ArrayList<Integer> list = new ArrayList<>();
ExecutorService executor = Executors.newCachedThreadPool();
Future<Integer> future = null;
Callable<Integer> callable = null;
Future<Integer> future;

for (int i = 1; i < 10; i++) {
future = executor.submit(new MyCallable(i));
try {
Expand All @@ -50,7 +45,9 @@ public static void main(String[] args) throws InterruptedException {
}

executor.shutdown();
//this is ont necessary in this case .. but .. good practice :)
executor.awaitTermination(1, TimeUnit.DAYS);

for (int i = 0; i < list.size(); i++) {
//get returned values from call()
System.out.println("List Values " + i + " Value: " + list.get(i));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
package CallableAndFuture_13;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.*;

/**
* Source:http://java-x.blogspot.com.tr/2006/11/java-5-concurrency-callable-and-future.html
* Till Java 1.4, threads could be implemented by either implementing Runnable
* or extending Thread. This was quite simple, but had a serious limitation -
* Source:
* <a href="http://java-x.blogspot.com.tr/2006/11/java-5-concurrency-callable-and-future.html">
* http://java-x.blogspot.com.tr/2006/11/java-5-concurrency-callable-and-future.html
* </a>
* <p>
* Till Java 1.4, threads could be implemented by either implementing
* {@link java.lang.Runnable} or extending {@link java.lang.Thread}.
* This was quite simple, but had a serious limitation;
* They have a run method that cannot return values. In order to side-step this,
* most programmers use side-effects (writing to a file etc.) to mimic returning
* values to the invoker of the thread. Java 5 introduces the Callable
* interface, that allows users to return values from a thread
*
* Runnable vs Callable<T>
* Runnable Introduced in Java 1.0 Callable<T> Introduced in Java 1.5 as part of
* java.util.concurrent library
*
* Runnable cannot be parametrized Callable is a parametrized type whose type
* parameter indicates the return type of its run method Classes implementing
*
* values to the invoker of the thread. Java 5 introduces the
* {@link java.util.concurrent.Callable} interface, that allows users to
* return values from a thread.
* </p>
* <p>
* {@link java.lang.Runnable} vs {@link java.util.concurrent.Callable} :
* <ul>
* <li>
* Runnable Introduced in Java 1.0. Callable<T> Introduced in Java 1.5 as
* part of
* {@link java.util.concurrent} library.
* </li>
* <li>
* Runnable cannot be parametrized .Callable is a parametrized type whose type
* parameter indicates the return type of its run method Classes implementing.
* </li>
* <li>
* Runnable needs to implement run() method, classes implementing Callable needs
* to implement call() method
*
* Runnable.run() returns no value, Callable.call() returns a value of Type T
*
* to implement call() method.
* </li>
* <li>
* Runnable.run() returns no value, Callable.call() returns a value of Type T.
* </li>
* <li>
* Runnable can not throw checked exceptions, Callable can throw checked
* exceptions
* exceptions.
* </li>
* </ul>
* </p>
*
* @author Z.B. Celik <[email protected]>
*/
Expand Down Expand Up @@ -58,20 +72,17 @@ public void setMyName(int myName) {

public class CallableTester {

public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {

Callable<Integer> callable = new CallableImpl(2);
ExecutorService executor = new ScheduledThreadPoolExecutor(1);
Future<Integer> future = executor.submit(callable);

try {
System.out.println("Future value: " + future.get());
} catch (Exception e) {
e.printStackTrace();
} finally {
executor.shutdown();
executor.isTerminated();
}
} catch (Exception ignored) {}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.HOURS);
}

}
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@

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


Expand Down