Skip to content

Commit b83db5e

Browse files
committed
JavaDoc optimized
Added myself to README.md
1 parent bfa2172 commit b83db5e

File tree

3 files changed

+53
-48
lines changed

3 files changed

+53
-48
lines changed

JavaMultiThreadingCodes/src/CallableAndFuture_13/App2.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
package CallableAndFuture_13;
22

3+
import java.util.ArrayList;
4+
import java.util.concurrent.*;
5+
36
/**
47
* Understanding Callable
58
*
69
* @author Z.B. Celik <[email protected]>
710
*/
8-
import java.util.ArrayList;
9-
import java.util.concurrent.Callable;
10-
import java.util.concurrent.ExecutionException;
11-
import java.util.concurrent.ExecutorService;
12-
import java.util.concurrent.Executors;
13-
import java.util.concurrent.Future;
14-
import java.util.concurrent.TimeUnit;
15-
1611
class MyCallable implements Callable<Integer> {
1712

18-
Integer value;
13+
int value;
1914

20-
public MyCallable(Integer i) {
15+
public MyCallable(int i) {
2116
this.value = i;
2217
}
2318

@@ -38,8 +33,8 @@ public class App2 {
3833
public static void main(String[] args) throws InterruptedException {
3934
ArrayList<Integer> list = new ArrayList<>();
4035
ExecutorService executor = Executors.newCachedThreadPool();
41-
Future<Integer> future = null;
42-
Callable<Integer> callable = null;
36+
Future<Integer> future;
37+
4338
for (int i = 1; i < 10; i++) {
4439
future = executor.submit(new MyCallable(i));
4540
try {
@@ -50,7 +45,9 @@ public static void main(String[] args) throws InterruptedException {
5045
}
5146

5247
executor.shutdown();
48+
//this is ont necessary in this case .. but .. good practice :)
5349
executor.awaitTermination(1, TimeUnit.DAYS);
50+
5451
for (int i = 0; i < list.size(); i++) {
5552
//get returned values from call()
5653
System.out.println("List Values " + i + " Value: " + list.get(i));
Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
package CallableAndFuture_13;
22

3-
import java.util.concurrent.Callable;
4-
import java.util.concurrent.ExecutorService;
5-
import java.util.concurrent.Future;
6-
import java.util.concurrent.ScheduledThreadPoolExecutor;
3+
import java.util.concurrent.*;
74

85
/**
9-
* Source:http://java-x.blogspot.com.tr/2006/11/java-5-concurrency-callable-and-future.html
10-
* Till Java 1.4, threads could be implemented by either implementing Runnable
11-
* or extending Thread. This was quite simple, but had a serious limitation -
6+
* Source:
7+
* <a href="http://java-x.blogspot.com.tr/2006/11/java-5-concurrency-callable-and-future.html">
8+
* http://java-x.blogspot.com.tr/2006/11/java-5-concurrency-callable-and-future.html
9+
* </a>
10+
* <p>
11+
* Till Java 1.4, threads could be implemented by either implementing
12+
* {@link java.lang.Runnable} or extending {@link java.lang.Thread}.
13+
* This was quite simple, but had a serious limitation;
1214
* They have a run method that cannot return values. In order to side-step this,
1315
* most programmers use side-effects (writing to a file etc.) to mimic returning
14-
* values to the invoker of the thread. Java 5 introduces the Callable
15-
* interface, that allows users to return values from a thread
16-
*
17-
* Runnable vs Callable<T>
18-
* Runnable Introduced in Java 1.0 Callable<T> Introduced in Java 1.5 as part of
19-
* java.util.concurrent library
20-
*
21-
* Runnable cannot be parametrized Callable is a parametrized type whose type
22-
* parameter indicates the return type of its run method Classes implementing
23-
*
16+
* values to the invoker of the thread. Java 5 introduces the
17+
* {@link java.util.concurrent.Callable} interface, that allows users to
18+
* return values from a thread.
19+
* </p>
20+
* <p>
21+
* {@link java.lang.Runnable} vs {@link java.util.concurrent.Callable} :
22+
* <ul>
23+
* <li>
24+
* Runnable Introduced in Java 1.0. Callable<T> Introduced in Java 1.5 as
25+
* part of
26+
* {@link java.util.concurrent} library.
27+
* </li>
28+
* <li>
29+
* Runnable cannot be parametrized .Callable is a parametrized type whose type
30+
* parameter indicates the return type of its run method Classes implementing.
31+
* </li>
32+
* <li>
2433
* Runnable needs to implement run() method, classes implementing Callable needs
25-
* to implement call() method
26-
*
27-
* Runnable.run() returns no value, Callable.call() returns a value of Type T
28-
*
34+
* to implement call() method.
35+
* </li>
36+
* <li>
37+
* Runnable.run() returns no value, Callable.call() returns a value of Type T.
38+
* </li>
39+
* <li>
2940
* Runnable can not throw checked exceptions, Callable can throw checked
30-
* exceptions
41+
* exceptions.
42+
* </li>
43+
* </ul>
44+
* </p>
3145
*
3246
* @author Z.B. Celik <[email protected]>
3347
*/
@@ -58,20 +72,17 @@ public void setMyName(int myName) {
5872

5973
public class CallableTester {
6074

61-
public static void main(String[] args) {
75+
public static void main(String[] args) throws InterruptedException {
6276

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

6781
try {
6882
System.out.println("Future value: " + future.get());
69-
} catch (Exception e) {
70-
e.printStackTrace();
71-
} finally {
72-
executor.shutdown();
73-
executor.isTerminated();
74-
}
83+
} catch (Exception ignored) {}
84+
executor.shutdown();
85+
executor.awaitTermination(1, TimeUnit.HOURS);
7586
}
7687

7788
}

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11

2-
Java Multithreading
3-
=============================================================
2+
#Java Multithreading
43
This repository will contain all the codes for the ultimate Java multithreading course by John Purcell
54

65
See the [Video Tutorials](https://www.udemy.com/java-multithreading/)
76
for more information.
87

9-
Contributor
10-
----------
11-
[Z.B. Celik] (http://www.linkedin.com/in/berkaycelik)
8+
##Contributors
9+
[Z.B. Celik] (http://www.linkedin.com/in/berkaycelik)
1210
[@IOAyman] (https://twitter.com/IOAyman)
1311

1412

15-
Java Multithreading Topics:
16-
-------------
13+
##Java Multithreading Topics:
1714
Codes with minor comments are from http://www.caveofprogramming.com/youtube/ also freely available at https://www.udemy.com/java-multithreading/?couponCode=FREE
1815

1916

0 commit comments

Comments
 (0)