author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:54:28 +0200 | |
changeset 104 | 0ec9eb8bcd03 |
permissions | -rw-r--r-- |
jtulach@104 | 1 |
package org.apidesign.deadlock; |
jtulach@104 | 2 |
|
jtulach@104 | 3 |
import java.util.concurrent.ExecutorService; |
jtulach@104 | 4 |
import java.util.concurrent.Executors; |
jtulach@104 | 5 |
import java.util.concurrent.Future; |
jtulach@104 | 6 |
import java.util.concurrent.TimeUnit; |
jtulach@104 | 7 |
import org.junit.Assert; |
jtulach@104 | 8 |
import org.junit.Test; |
jtulach@104 | 9 |
|
jtulach@104 | 10 |
public class ExecutorServiceTest implements Runnable { |
jtulach@104 | 11 |
private int cnt; |
jtulach@104 | 12 |
|
jtulach@104 | 13 |
@Test |
jtulach@104 | 14 |
public void showUsageOfExecutionService() throws Exception { |
jtulach@104 | 15 |
Runnable someRunnable = this; |
jtulach@104 | 16 |
|
jtulach@104 | 17 |
// BEGIN: deadlock.service |
jtulach@104 | 18 |
ExecutorService service = Executors.newSingleThreadExecutor(); |
jtulach@104 | 19 |
|
jtulach@104 | 20 |
// scheduling is quite different |
jtulach@104 | 21 |
Future<?> task = service.submit(someRunnable); |
jtulach@104 | 22 |
|
jtulach@104 | 23 |
// compared to waiting |
jtulach@104 | 24 |
task.get(1000, TimeUnit.MILLISECONDS); |
jtulach@104 | 25 |
// END: deadlock.service |
jtulach@104 | 26 |
|
jtulach@104 | 27 |
Assert.assertEquals("Cnt executed", 1, cnt); |
jtulach@104 | 28 |
} |
jtulach@104 | 29 |
|
jtulach@104 | 30 |
public void run() { |
jtulach@104 | 31 |
cnt++; |
jtulach@104 | 32 |
} |
jtulach@104 | 33 |
|
jtulach@104 | 34 |
} |