Written by me. :-)
Stopping a thread made easy
import java.util.concurrent.*;
public class StopExample {
public static void main(String[] args) throws InterruptedException {// This executor creates a thread if there are no thread available.
// If there are previously created but idle threads, reuse them.
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(
new Runnable() {
public void run() {
try {
while(true) {
Thread.sleep(10);
System.out.println(“##”);
}
} catch(InterruptedException ie) {}
}
});
for (int i = 100 ; i >= 0; i–) {
System.out.println(i);
Thread.sleep(1);
}
exec.shutdownNow();
}}
Asynchronous function call: In JDK 1.4 or below,it’s not easy to correctly implement codes equivalent to the following.
import java.util.concurrent.*;
public class AsyncExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService exec = Executors.newCachedThreadPool();
Callable<Integer> c = new Callable<Integer>() {
public Integer call() {// after long computation
try { Thread.sleep(1000); } catch(InterruptedException ioe) {}
return 1;
}
};Future<Integer> result = exec.submit(c);
while (result.isDone() == false) {
System.out.println(“Not yet.”);
}System.out.println(“We’ve got: ” + result.get());
// exec should be shutdown to stop this program
exec.shutdown();
}
}
Producer Consumer based on a priority queue
import java.util.*;
import java.util.concurrent.*;public class ProducerConsumerExample {
public static void main(String[] args) {
// creates a unbounded threadsafe queue
final PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<Integer>();
final Random r = new Random();// A thread pool which reuses previously created threads
ExecutorService exec = Executors.newCachedThreadPool();// Latch for starting threads
final CountDownLatch latch = new CountDownLatch(10);Runnable producer = new Runnable() {
public void run() {// wait for start signal
try { latch.await(); } catch(InterruptedException ie) {}System.out.println(“Producer started.”);
for (int i = 0; i < 10; i++) {
int value = r.nextInt() % 10;
queue.offer(value);
System.out.println(“Produced: ” + value);try { Thread.sleep(Math.abs(r.nextInt() % 100)); } catch(InterruptedException ie) {}
}
}
};Runnable consumer = new Runnable() {
public void run() {// wait for start signal
try { latch.await(); } catch(InterruptedException ie) {}System.out.println(“Consumer started.”);
for (int i = 0; i < 10; i++) {
Integer value = queue.poll();
if (value != null)
System.out.println(“Consumed: ” + value);try { Thread.sleep(Math.abs(r.nextInt() % 100)); } catch(InterruptedException ie) {}
}
}
};exec.execute(producer);
exec.execute(consumer);// Threads are not started yet.
for (int i = 10; i >= 0; i–) {
System.out.println(“Counting down: ” + i);
try { Thread.sleep(200); } catch(InterruptedException ie) {}
latch.countDown();
}exec.shutdown();
}
}
Sample Output: Note that smaller values are consumed earlier.
[pool007@dke tmp]$ java ProducerConsumerExample
Counting down: 10
Counting down: 9
Counting down: 8
Counting down: 7
Counting down: 6
Counting down: 5
Counting down: 4
Counting down: 3
Counting down: 2
Counting down: 1
Counting down: 0
Producer started.
Produced: -7
Consumer started.
Consumed: -7
Produced: 7
Consumed: 7
Produced: 4
Produced: -4
Consumed: -4
Produced: 6
Consumed: 4
Consumed: 6
Produced: 7
Produced: -7
Consumed: -7
Consumed: 7
Produced: 4
Consumed: 4
Produced: -9
Produced: 2
Consumed: -9
Leave a Reply