多线程测试实例

import java.util.HashSet;
import java.util.Set;

public class ThreadDemo extends Thread
{
    private static Object wait;
    private static     int threads = 250;
    private static int size = 1000;

    public ThreadDemo(){
       
    }
    public void run()
    {
        synchronized (wait) {
            try {
                wait.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < size; i++) {
            long starttime = System.currentTimeMillis();
            // 处理的业务逻辑
            long endTime = System.currentTimeMillis();
            long result = endTime - starttime;
            System.out.println("thredName " + Thread.currentThread().getName()
                    + " time:" + result);
        }
    }
   
    public static void main(String args[]) throws Exception{
            wait = new Object();
            Set<ThreadDemo> ts = new HashSet<ThreadDemo>();
            for(int i = 0; i < threads; i++)
            {
                ThreadDemo t = new ThreadDemo();
                ts.add(t);
                t.start();           
            }
            synchronized (wait) {
                wait.notifyAll();
            }
    }
}

你可能感兴趣的:(多线程)