我是靠谱客的博主 发嗲水蜜桃,最近开发中收集的这篇文章主要介绍JAVA 模拟多用户提交动作,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

为了测试项目里的多线程操作,在网上找的一个模拟多线程的一个程序。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;

import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class ConcurrentTest {

    private static int thread_num = 4;
    private static int client_num = 4;
    private static Map keywordMap = new HashMap();

    static {
        try {
            /*InputStreamReader isr = new InputStreamReader(new FileInputStream(
                    new File("clicks.txt")), "GBK");
            BufferedReader buffer = new BufferedReader(isr);
            String line = "";
            while ((line = buffer.readLine()) != null) {
                keywordMap.put(line.substring(0, line.lastIndexOf(":")), "");
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    
    public static void main(String[] args) {

        int size = keywordMap.size();

        // TODO Auto-generated method stub
        ExecutorService exec = Executors.newCachedThreadPool();
        // 50个线程可以同时访问
        final Semaphore semp = new Semaphore(thread_num);
        // 模拟2000个客户端访问
        for (int index = 0; index < client_num; index++) {
            final int NO = index;
            Runnable run = new Runnable() {
                public void run() {
                    try {
                        // 获取许可
                        semp.acquire();
                        System.out.println("Thread:" + NO);
                        String host = "http://192.168.56.1:8088/bjkw/admin_contractInfoSave.do?type=approveRi&";
                        String para = "id=9084078";
                        System.out.println(host + para);
                        URL url = new URL(host);// 此处填写供测试的url
                        HttpURLConnection connection = (HttpURLConnection) url
                                .openConnection();
                         connection.setRequestMethod("POST");
                         connection.setRequestProperty("Proxy-Connection",
                         "Keep-Alive");
                        connection.setDoOutput(true);
                        connection.setDoInput(true);

                        PrintWriter out = new PrintWriter(connection
                                .getOutputStream());
                        out.print(para);
                        out.flush();
                        out.close();

                        BufferedReader in = new BufferedReader(
                                new InputStreamReader(connection
                                        .getInputStream()));

                        String line = "";
                        String result = "";
                        while ((line = in.readLine()) != null) {
                            result += line;
                        }

                        // System.out.println(result);
                        // Thread.sleep((long) (Math.random()) * 1000);
                        // 释放
                        System.out.println("第:" + NO + " 个");
                        semp.release();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            exec.execute(run);
        }
        // 退出线程池
        exec.shutdown();
    }

    private static String getRandomSearchKey(final int no) {
        String ret = "";
        int size = keywordMap.size();
        // int wanna = (int) (Math.random()) * (size - 1);
        ret = (keywordMap.entrySet().toArray())[no].toString();
        ret = ret.substring(0, ret.lastIndexOf("="));
        System.out.println("t" + ret);
        return ret;
    }
}

最后

以上就是发嗲水蜜桃为你收集整理的JAVA 模拟多用户提交动作的全部内容,希望文章能够帮你解决JAVA 模拟多用户提交动作所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部