用于模拟并发多用户多线程测试

package com;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

import com.yt.util.WIFICommonUtil;
import com.yt.wifi.util.Jdbc;

/**
 *aa.java
 *com
 *工程:wifilogin
 *功能:TODO
 *author  date time
 *李星星 2014-4-10 下午1:07:08
 *Email:[email protected]
 *copytight (C) 2014 ,INI All Rights Reserved.
 */
public class aa {

    private static int thread_num = 300;  
    private static int client_num = 1000;  
 
    public static void main(String[] args) {  
        ExecutorService exec = Executors.newCachedThreadPool();  
 
        final Semaphore semp = new Semaphore(thread_num);  
 
        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);  
                        //业务逻辑  
                        WIFICommonUtil util = new WIFICommonUtil();
                        Map info = WIFICommonUtil.getUsernameAndPassword("123ssdd"+NO, "a", "aa", "aa", "bb", "aA");
                        Jdbc.execute("INSERT into user_test(username,phone) VALUES('"+info.get("username")+"','1236454"+NO+"')");
                        semp.release();  //释放
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                }  
            };  
            exec.execute(run);  
        }  
        exec.shutdown();  
    }  

}

你可能感兴趣的:(文章)