51Testing软件测试论坛

标题: Java 程序模仿多用户并发 [打印本页]

作者: stonemary    时间: 2010-3-8 21:34
标题: Java 程序模仿多用户并发
import java.util.ArrayList;
import java.util.List;


public class TestThread {
       
        static List<Prameter> prameterList = new ArrayList<Prameter>();
       
        static {
                for(int i=0;i<10;i++){
                        prameterList.add(new Prameter("a"+i,"b"+i));
                }
               
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
               
                for(int i=0;i<10;i++){
                        new MyThread(i,prameterList.get(i)).start();
                }
        }

}

class Prameter {
        private String a;
       
        private String b;

        public String getA() {
                return a;
        }

        public void setA(String a) {
                this.a = a;
        }

        public String getB() {
                return b;
        }

        public void setB(String b) {
                this.b = b;
        }

        public Prameter(String a, String b) {
                super();
                this.a = a;
                this.b = b;
        }
       
}

class MyThread extends Thread{
       
        private int i;
       
        private Prameter prameter;
       
        public MyThread (int i,Prameter prameter){
                this.i = i;
                this.prameter = prameter;
                System.out.println(this.getName()+" inited.");
        }
       
        public void run(){
                try {
                        this.sleep(10*1000);                       
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
                long startTime = System.currentTimeMillis();
                this.processTest();               
                long endTime = System.currentTimeMillis();
                System.out.println(this.getName()+" Thread Run time:"+(endTime-startTime)+" ms");
        }
       
        private void processTest(){
                new Client().test(this.prameter.getA()+"  -> "+ this.prameter.getB());
        }
       
}



class Client {
       
        public void test(String s){
                System.out.println(s);               
        }
}




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2