forked from learning-zone/java-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallableExample.java
More file actions
32 lines (24 loc) · 789 Bytes
/
CallableExample.java
File metadata and controls
32 lines (24 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package multithreading;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class CallableExample implements Callable {
public Object call() throws Exception {
Random generator = new Random();
Integer randomNumber = generator.nextInt(5);
Thread.sleep(randomNumber * 100);
return randomNumber;
}
public static void main(String args[]) throws Exception {
FutureTask[] randomNumberTasks = new FutureTask[5];
for(int i = 0; i < 5; i++) {
Callable callable = new CallableExample();
randomNumberTasks[i] = new FutureTask(callable);
Thread t = new Thread(randomNumberTasks[i]);
t.start();
}
for(int i = 0; i < 5; i++){
System.out.println("FutureTask: "+randomNumberTasks[i].get());
}
}
}