package com.ann.demo.entity.other;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ServerHelper {
    private static ReentrantReadWriteLock lckClass;
    private static boolean threadIsRunning;
    private static BlockingQueue<RunningItem> ItemList;
    private static Thread thread;
    private static final Logger logger = LoggerFactory.getLogger(ServerHelper.class);

    public ServerHelper() {
        lckClass = new ReentrantReadWriteLock();
        threadIsRunning = false;
        ItemList = new LinkedBlockingQueue<RunningItem>();
        this.thread = null;
    }

    public static ServerHelper _instance = null;

    public static ServerHelper getInstance() {
        if (_instance == null) {
            _instance = new ServerHelper();
        }
        return _instance;
    }

    private synchronized void AppendItemList(RunningItem item) throws InterruptedException {
        ItemList.put(item);
    }

    private synchronized RunningItem PollItemList() {
        return ItemList.poll();
    }

    private synchronized long ItemListCount() {
        return ItemList.size();
    }

    //处理接口
    private void ProcessItem(RunningItem item) throws InterruptedException {
        //System.out.println("执行第 "+ItemListCount()+" 个对象");
        item.Save();
        //System.out.println("执行结果=>保存成功");
    }

    public void Start(RunningItem item) {
        try {
            //添加到队列
            AppendItemList(item);

            //判断线程是否启动
            lckClass.readLock().lock();
            try {
                if (threadIsRunning) {
                    return;
                }
                threadIsRunning = true;
            } finally {
                lckClass.readLock().unlock();
            }
            if (thread == null) {
                thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            while (true) {
                                RunningItem item = PollItemList();
                                try {
                                    if (item == null) {
                                        continue;
                                    }
                                    //真正的处理代码
                                    ProcessItem(item);
                                } catch (Exception e2) {
                                    if (item != null) {
                                        AppendItemList(item);
                                    }
                                }
                            }
                        } catch (Exception e1) {
                            logger.error("ServerHelper  Start Error:" + e1);
                        }
                    }
                });
            }
            thread.start();
        } catch (Exception e) {
            logger.error("ServerHelper Start Error1:" + e);
        }
    }


    public void Stop() {
        try {
            lckClass.readLock().lock();
            try {
                if (!threadIsRunning) {
                    return;
                }
                threadIsRunning = false;
            } finally {
                lckClass.readLock().unlock();
            }

            if (thread != null) {
                thread.stop();
            }
        } catch (Exception e) {
            logger.error("ServerHelper Stop Error:" + e);
        }
    }
}


