JAVA模拟高并发

import java.util.concurrent.CountDownLatch;

/**
 * 模拟高并发
 * User: Scott
 * Date: 2018/4/14
 * Time: 20:41
 */
public class UserDo implements Runnable {
    // 并发的用户数(同时并发的线程数)
    private static final int threadNum = 1;
    // 倒计数,用于制造线程的并发执行
    private static CountDownLatch countDownLatch = new CountDownLatch(threadNum);

    private String msg;
    public UserDo(String msg) {
        this.msg = msg;
    }

    @Override
    public void run() {
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(msg);
    }

    public static void main(String[] args) {
        for(int i=0; i< threadNum; i++) {
            new Thread(new UserDo("do something " + i)).start();
            countDownLatch.countDown();
        }
    }
}

Java模拟高并发的问题

08-31

[code=java]rnpublic class Request implements Runnable rnrn @Overridern public void run() rn // TODO Auto-generated method stubrn PrintWriter pout = null;rn BufferedReader pin = null;rn try rn URL realUrl = new URL("http://127.0.0.1:8080/zyhCityTcp/user!getById?id=3");rn // 打开和URL之间的连接rn URLConnection conn = realUrl.openConnection();rn // 设置通用的请求属性rn conn.setRequestProperty("accept", "*/*");rn conn.setRequestProperty("connection", "Keep-Alive");rn conn.setRequestProperty("user-agent",rn "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");rn rn conn.connect();rn // 定义BufferedReader输入流来读取URL的响应rn pin = new BufferedReader(rn new InputStreamReader(conn.getInputStream(),"utf-8"));rn String line;rn boolean print = false;rn StringBuilder sb = new StringBuilder("");rn while ((line = pin.readLine()) != null) rn sb.append(line);rn rn catch (Exception e) rn //pout.println("发送 POST 请求出现异常!"+e.getMessage());rn e.printStackTrace();rn rn //使用finally块来关闭输出流、输入流rn finallyrn tryrn if(pout != null)rn pout.close();rn rn if(pin != null)rn pin.close();rn rn rn catch(IOException ex)rn ex.printStackTrace();rn rn rn rnrn public static void main(String[] args) rn // TODO Auto-generated method stubrn ExecutorService pool = Executors.newCachedThreadPool();rn Request req = new Request();rn for(int i = 0;i < 100;i ++) rn pool.execute(req);rn rn rnrnrn[/code]rn我建立了100个线程去访问服务器,运行结果,服务器有响应的,并且也有传回数据的操作,但是客户端Java程序却接收不到响应。为什么会这样呢?应该怎么获取到响应信息?

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试