黑马程序员技术交流社区
标题:
测试多线程执行时间
[打印本页]
作者:
oath
时间:
2013-8-16 11:52
标题:
测试多线程执行时间
本帖最后由 oath 于 2013-8-17 07:16 编辑
package test;
abstract class Temple{
public void getTime(){
long start = System.currentTimeMillis();
System.out.println("程序开始时间:" + start);
runcode();
long end = System.currentTimeMillis();
System.out.println("程序结束时间:"+end);
System.out.println("程序用时:"+(end-start));
}
abstract void runcode() ;
}
class SaleTicket implements Runnable{
public void run(){
int ticket = 1000;
for (int i = 0; i < 1000; i++) {
if( ticket > 0 ) {
System.out.println(Thread.currentThread().getName()+": "+ticket--);
}
}
}
}
class Temple_Thread extends Temple{
@Override
void runcode() {
// TODO Auto-generated method stub
SaleTicket st = new SaleTicket();
Thread t1 = new Thread(st,"one");
Thread t2 = new Thread(st,"two");
Thread t3 = new Thread(st,"third");
t1.start();
t2.start();
t3.start();
}
}
class test{
public static void main(String[] args) {
new Temple_Thread().getTime();
}
}
复制代码
上面的程序,多次测试,结果程序运行时间都为0,不应该啊……
大家帮我看看哪里写错了
作者:
funneies
时间:
2013-8-16 13:35
可能程序出错了,你看看。
abstract class Temple{
public void getTime(){
long start = System.currentTimeMillis();
System.out.println("程序开始时间:" + start);
runcode();
long end = System.currentTimeMillis();
System.out.println("程序结束时间:"+end);
System.out.println("程序用时:"+(end-start));
}
abstract void runcode() ;
}
class SaleTicket implements Runnable{
public void run(){
int ticket = 1000;
for (int i = 0; i < 1000; i++) {
if( ticket > 0 ) {
System.out.println(Thread.currentThread().getName()+": "+ticket--);
}
}
}
}
class Temple_Thread extends Temple{
@Override
void runcode() {
// TODO Auto-generated method stub
SaleTicket st = new SaleTicket();
Thread t1 = new Thread(st,"one");
Thread t2 = new Thread(st,"two");
Thread t3 = new Thread(st,"third");
t1.start();
t2.start();
t3.start();
}
}
public class Test{
public static void main(String[] args) {
new Temple_Thread().getTime();
}
}
复制代码
运行结果:
程序开始时间:1376630931500
one: 1000
程序结束时间:1376630931534
程序用时:34
third: 1000
third: 999
third: 998
two: 1000
third: 997
。。。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2