Java testing concurrency

Source: Internet
Author: User
/*** Contest. java * copyright (c) 2012 * created: cuiran 10:12:14 */package COM. wpndemo. test; 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. iterator; import Java. util. set; import Java. util. map; import Java. util. con Current. executorservice; import Java. util. concurrent. executors; import Java. util. concurrent. semaphore; public class contest {Private Static int thread_num = 10; Private Static int client_num = 10; Private Static int num = 0; public static int getno () {num = num + 1; system. out. println (Num); Return num;} Private Static map keywordmap = new hashmap (); static {// try {/// inputstreamreader ISR = new inputstreamreader (n EW 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 stubexecutorservice exec = executors. newcachedthreadpool (); keywordmap. put ("test01", "38338"); keywordmap. put ("test02"," 38339 "); keywordmap. put ("test03"," 38340 "); keywo Rdmap. put ("test04"," 38341 "); keywordmap. put ("test05", "38342"); // keywordmap. put ("test06"," 38343 "); // keywordmap. put ("test07"," 38344 "); // keywordmap. put ("test08", "38345"); // keywordmap. put ("test09"," 38346 "); // keywordmap. put ("test10", "38347"); // 50 threads can simultaneously access final semaphore SEMP = new semaphore (thread_num ); // simulate access from 2000 clients to set = keywordmap. keyset (); // iterator it = set. iterator (); int Index = 0; while (It. hasnext () {string username = (string) it. next (); // final string pass = keywordmap. get (username ). tostring (); // runnable run = new runnable () {int NO = concurrenttest. num; Public void run () {try {// obtain the license SEMP. acquire (); system. out. println ("thread:" + NO); string host = "http: // 192.168.0.20.: 8080/messageweb/messagesrevlet. do? "; String para = "m = reqcalltaxi & id =" + pass + "& Key = 35bc7f25daa881fa0974730276868f12 & phone = 13401038652 & BX = 116.311754 & by = 40.034496 &" + "blocation = % E4 % b8 % 8A % E5 % 9C % B0 & BDEs = % E5 % 8C % 97% E4 % Ba % AC % E5 % B8 % 82% E6 % B5 % B7 % E6 % B7 % 80% e5 % 8C % Ba % E4 % B8 % 8A % E5 % 9C % b03 % E8 % a1 % 97% E7 % 8e % af % E5 % B2 % 9B % E8 % a5 % BF % E5 % 8d % 97% E8 % A7 % 92 (% E8 % BF % 91% E4 % B8 % 8A % E5 % 9C % B0 % E7 % 8e % af % E5 % B2 % 9b) & "+" EX = 116.315942 & ey = 40.05343 & elocation = % E8 % a5 % BF % E4 % Ba % 8C % E6 % 9 7% 97 & edes = 333% E8 % B7 % af % E5 % 86% E7 % 8e % AF, 85% E8 % B7 % af % E5 % A4 % 333% E7 % 8e % AF, 392% E8 % B7 % AF, % E8 % BF % 90% E9 % 80% 9a114% E8 % B7 % AF, 636% E8 % B7 % AF & "+" time = 15 & usernum = 1 & issalute = 1 & name = % E6 % 9C % B1 % E5 % 85% E7 % 88% 9f & level = 3 & Credit =-3 & bytime = 2012-07-12% 6:6:34:17 & type = 0 ¬ ifytime = 2012-07-12% 6:6:34:17 & s = 123456 & "+" jsoncallback = Hangzhou & _ = 1342059557372"; system. out. println (host + para); Url u RL = new URL (host); // enter the URL for testing. 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 Bu Fferedreader (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); // release system. out. println (":" + NO + ""); SEMP. release ();} catch (exception e) Release e.printstacktrace({{}}{exec.exe cute (run);} // exit the thread pool exec. shutdown ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.