使用的ThreadPoolExecutor和DiscardPolicy(Using ThreadPo

2019-10-23 03:10发布

我需要做一个客户端队列的ThreadPoolExecutor和下降的客户的能力,如果超过一定数目(例如5)。 它是有点儿DDOS防护。 当客户端#6请求我的服务器 - 它得到了下降,等我有我的服务器和客户端的代码,但我不知道如何实现的ThreadPoolExecutor和DiscardPolicy。 想法或例子?

简单的服务器:

   import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Semaphore;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Server {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        ServerSocket server = new ServerSocket (3000);

        ExecutorService es = Executors.newFixedThreadPool(2);

        Semaphore semaphore = new Semaphore (2);

        while(true){

        semaphore.acquire();

        Socket accept2 = server.accept();

        es.execute(()->{
            try (Socket accept = accept2) {
            serve(accept);
            } catch (Exception exception) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, exception);
            } 
            finally {
                semaphore.release();

            }
        });        

        }

    }

    private static void serve(final Socket accept) throws ClassNotFoundException, IOException {
        InputStream inputStream = accept.getInputStream();
        OutputStream outputStream = accept.getOutputStream();

        ObjectInputStream inputStream2 = new ObjectInputStream (inputStream);

        while (true){
            Object readObject = inputStream2.readObject();
            System.out.println(readObject);
        }

        }

    }

而一个简单的客户端:

  import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;

    public class Client {

    public static void main(String[] args) throws IOException, InterruptedException {
        Socket socket = new Socket ("localhost", 3000);
        ObjectOutputStream oos = new ObjectOutputStream (
                socket.getOutputStream());
        oos.writeObject("First!");
        Thread.sleep(10000);
        oos.writeObject("First again!");
        Thread.sleep(10000);
        oos.writeObject("First again again!");

        }

    }

Answer 1:

使用ThreadPoolExecutorDiscardPolicy如下:

  int poolSize=1;
  int maxPoolSize=2;
  int queueSize=5;
  long aliveTive=1000;
  ArrayBlockingQueue<Runnable> queue= new ArrayBlockingQueue<Runnable>(queueSize);
  ThreadPoolExecutor executor= new ThreadPoolExecutor(poolSize,maxPoolSize,aliveTive,
                    TimeUnit.MILLISECONDS,queue,new ThreadPoolExecutor.DiscardPolicy());
}

被拒绝的任务:

在方法提交新的任务执行(Runnable接口),将在执行程序已关闭了拒绝,并且也当执行器使用有限的边界两个最大线程和工作队列容量,且饱和。

在任一情况下,执行方法调用RejectedExecutionHandler.rejectedExecution(Runnable, ThreadPoolExecutor)其的方法RejectedExecutionHandler

提供四个预定义的处理程序策略:

  1. 在默认ThreadPoolExecutor.AbortPolicy ,处理程序抛出时甩运行时RejectedExecutionException。
  2. ThreadPoolExecutor.CallerRunsPolicy ,调用执行自身的线程运行的任务。 这提供了一种简单的反馈控制机制,这将减缓新任务的提交速度。
  3. ThreadPoolExecutor.DiscardPolicy ,不能执行的任务仅仅是被丢弃。
  4. ThreadPoolExecutor.DiscardOldestPolicy ,如果执行程序没有关闭,在工作队列头部的任务将被丢弃,然后执行重试(可再次失败,导致此重复。)

看看这个文档页面了解详情



文章来源: Using ThreadPoolExecutor and DiscardPolicy