Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- public class Test {
- private BlockingQueue<String> queue;
- public Test(){
- queue = new ArrayBlockingQueue<String>(10);
- for (int i=0 ;i<10 ;i++ ){
- queue.add(new String("" + i));
- }
- // Create a set of worker threads
- Worker[] workers = new Worker[2];
- for (int i = 0; i < workers.length; i++) {
- workers[i] = new Worker();
- workers[i].start();
- }
- }
- private class Worker extends Thread {
- public void run() {
- while (true) {
- String item = queue.poll();
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("item:" + item);
- }
- }
- }
- public static void main(String[] args) {
- new Test();
- }
- }
Add Comment
Please, Sign In to add comment