-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c61407
commit e285545
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
java/threads/src/main/java/com/bejond/threads/callable/ReturnWithResultThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.bejond.threads.callable; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* Created by bejond on 4/16/16. | ||
* | ||
* If we want a thread return a value, we can make the thread implement | ||
* Callable<T>. | ||
*/ | ||
public class ReturnWithResultThread implements Callable { | ||
private long id; | ||
|
||
public ReturnWithResultThread(long id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public Object call() throws Exception { | ||
System.out.println("Invoke call()" + id); | ||
return "The thread return result is " + id; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
java/threads/src/test/java/com/bejond/threads/callable/TestReturnWithResultThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.bejond.threads.callable; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
|
||
/** | ||
* Created by bejond on 4/16/16. | ||
*/ | ||
public class TestReturnWithResultThread { | ||
|
||
@Test | ||
public void testReturnThread() { | ||
|
||
/** | ||
* Executors.newCachedThreadPool() returns a ThreadPoolExecutor instance, | ||
* ThreadPoolExecutor extends AbstractExecutorService, and | ||
* AbstractExecutorService implements ExecutorService. | ||
*/ | ||
ExecutorService executorService = Executors.newCachedThreadPool(); | ||
|
||
ArrayList<Future<String>> futureArrayList = | ||
new ArrayList<Future<String>>(); | ||
|
||
for (int i = 0; i < 100; i++) { | ||
|
||
/** | ||
* AbstractExecutorService implements submit(Callable<T>). | ||
* submit() invoke call(). | ||
*/ | ||
futureArrayList.add( | ||
executorService.submit(new ReturnWithResultThread(i))); | ||
} | ||
|
||
System.out.println("Adding threads is done."); | ||
|
||
for (Future<String> future : futureArrayList) { | ||
try { | ||
// get() blocks until completing task | ||
System.out.println(future.get()); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} catch (ExecutionException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
executorService.shutdown(); | ||
} | ||
} | ||
} | ||
} |