long time = new Date().getTime();//获取启动时的时间. System.out.println(time);这上面这个可以获取时间(s).不懂的话,参考下api吧。
在xx类的构造方法获取开始时间,并获取for循环的j,如果j=0,获取开始时间,如果j=list.size()-1,则在构造的最后调用加入某XX构造方法 public XX(int x ,int size){ if(x=0){ System.out.println(time); } //执行体 if(x=size-1) System.out.println(time); } }
要是要求不高的话 用activeCount()吧 Date date1=new Date();//获取第一个时间 /*这里起线程*/ while(Thread.currentThread().activeCount()>1);//即除了main线程还有其他线程就循环。 Date date2=new Date();//获取第二个时间 System.out.println(date2.getTime()-date1.getTime());//得到的是毫秒,要转换就用SimpleDateFormat
时间精度要求不高,确实可以用。 currentThread()是Returns a reference to the currently executing thread object.如果在其他地方也开了线程不知道会有什么影响
利用CountDownLatch这个类,countDown和await这两个方法很有用
参考9楼,把你的线程在启动的时候放到指定的线程组里。 代码如下 public static void main(String[] args) { ThreadGroup thGroup = new ThreadGroup("MyTest");
long startTs = System.currentTimeMillis(); for(int i=0;i<10;i++){ final int id = i; Thread subThread = new Thread(thGroup, new Runnable(){ // Create a thread which will run random time 0~5 sec. public void run() { int myId = id; int sec = (int) (Math.random()*5000); try { Thread.sleep(sec); } catch (InterruptedException e) { } System.out.printf("Thread %d runed %f sec\n", myId, sec/1000.0); } }); subThread.start(); } // wait all thread down. while(thGroup.activeCount()>0){ try { Thread.sleep(20); } catch (InterruptedException e) { } } long endTs = System.currentTimeMillis();
isAlive()可以查看该线程是否结束。
可以在开始第一个线程的时候获得时间,期间循环检查是否有线程未结束,直到全部线程都结束,再次获得时间,做减法
System.out.println(time);这上面这个可以获取时间(s).不懂的话,参考下api吧。
public XX(int x ,int size){
if(x=0){
System.out.println(time);
}
//执行体
if(x=size-1)
System.out.println(time);
}
}
用activeCount()吧
Date date1=new Date();//获取第一个时间
/*这里起线程*/
while(Thread.currentThread().activeCount()>1);//即除了main线程还有其他线程就循环。
Date date2=new Date();//获取第二个时间
System.out.println(date2.getTime()-date1.getTime());//得到的是毫秒,要转换就用SimpleDateFormat
代码如下
public static void main(String[] args) {
ThreadGroup thGroup = new ThreadGroup("MyTest");
long startTs = System.currentTimeMillis();
for(int i=0;i<10;i++){
final int id = i;
Thread subThread = new Thread(thGroup, new Runnable(){
// Create a thread which will run random time 0~5 sec.
public void run() {
int myId = id;
int sec = (int) (Math.random()*5000);
try {
Thread.sleep(sec);
} catch (InterruptedException e) {
}
System.out.printf("Thread %d runed %f sec\n", myId, sec/1000.0);
}
});
subThread.start();
}
// wait all thread down.
while(thGroup.activeCount()>0){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
}
}
long endTs = System.currentTimeMillis();
System.out.printf("Total used %f sec\n", (endTs-startTs)/1000.0);
}
运行结果
Thread 6 runed 1.382000 sec
Thread 8 runed 1.655000 sec
Thread 2 runed 1.774000 sec
Thread 9 runed 1.981000 sec
Thread 0 runed 2.242000 sec
Thread 5 runed 2.640000 sec
Thread 1 runed 2.855000 sec
Thread 4 runed 2.899000 sec
Thread 3 runed 3.582000 sec
Thread 7 runed 4.184000 sec
Total used 4.224000 sec可以看到,基本上是运行时间最长的线程结束后就得到一个时间
class Driver { // ...
void main() throws InterruptedException {
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(N);
long startTs = System.currentTimeMillis();// 开始计时
for (int i = 0; i < N; ++i)
// create and start threads
new Thread(new Worker(startSignal, doneSignal)).start(); doSomethingElse(); // don't let run yet
startSignal.countDown(); // let all threads proceed
doSomethingElse();
doneSignal.await(); // wait for all to finish
long endTs = System.currentTimeMillis();// 结束计时 System.out.printf("Total used %f sec\n", (endTs - startTs) / 1000.0);
}
}class Worker implements Runnable {
private final CountDownLatch startSignal;
private final CountDownLatch doneSignal; Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
this.startSignal = startSignal;
this.doneSignal = doneSignal;
} public void run() {
try {
startSignal.await();
doWork();
doneSignal.countDown();
} catch (InterruptedException ex) {
} // return;
} void doWork() { ... }
}
//写了计数器
public class ThreadCount {
private int count = 0; public int getCount() {
return count;
} public void setCount(int count) {
this.count = count;
} public synchronized void plus() { // +1
count++;
} public synchronized void minu() {// -1
count--;
}
}public class FreemakerThread extends Thread{
private ThreadCount failure;
private ThreadCount success;
getxx();
setxx();
public void run() { try {
doSomething();
success.plus();//success+1
} catch (Exception e) {
e.printStackTrace();
failure.plus();//failure+1
}
}}while((failure.getCount+success.getCount)!=线程数){
Thread.sleep(10);
}
long endTs = System.currentTimeMillis();// 结束计时