Java ExecutorService 實現隊列消費

package MultiThread;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ListExample {
    private  static ArrayList<String>  list = new ArrayList<>();
    private  static  Object  lock = new Object();
    public static void main(String[] args) {

        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                //Thread.currentThread().setName("inputdata");
                synchronized (lock){
                    while (list.size()==0) {
                        list.add("data");
                        System.out.println(Thread.currentThread().getName()+":input data");
                        lock.notifyAll();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        });

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                //Thread.currentThread().setName("remove data");
                synchronized (lock){
                    while (list.size()==1){
                        try {
                            System.out.println(Thread.currentThread().getName()+":remove  data");
                            list.remove(0);
                            lock.notifyAll();
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }

            }
        });

    }

}
package MultiThread;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ConcurrentLinkedQueueExaple {
    private  static ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
    private  static  Object  lock = new Object();
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                //Thread.currentThread().setName("inputdata");
                synchronized (lock){
                    while (queue.isEmpty()) {
                        queue.add("data");
                        System.out.println(Thread.currentThread().getName()+":input data");
                        lock.notifyAll();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        });

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                //Thread.currentThread().setName("remove data");
                synchronized (lock){
                    while (!queue.isEmpty()){
                        try {
                            System.out.println(Thread.currentThread().getName()+":remove  data");
                            queue.poll();
                            lock.notifyAll();
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }

            }
        });

    }
    }

 

發佈了59 篇原創文章 · 獲贊 19 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章