日期:2014-05-20 浏览次数:20776 次
package tt;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ParaTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
/**
* 目的: 比较两种方法的速率快慢
* 第一种方法: 单独一个线程串行的向日志文件中写入2048个元素
* 第二种方法: 两个线程并行,一个线程生成2048个元素后,另外一个获取该容器的锁,并写入日志文件。
*
*/
/**
* case 1:
*/
PThread_1 p1 = new PThread_1();
Thread t1 = new Thread(p1);
long startTime = System.currentTimeMillis();
t1.start();
t1.join();
long endTime = System.currentTimeMillis();
System.out.println("The T1 running time is :" + (endTime - startTime));
/**
* case 2:
*/
PThread_2 p2 = new PThread_2();
PThread_3 p3 = new PThread_3();
Thread t2 = new Thread(p2);
Thread t3 = new Thread(p3);
t2.start();
t2.join();
startTime = System.currentTimeMillis();
t3.start();
t3.join();
endTime = System.currentTimeMillis();
System.out.println("The T3 running time is :"
+ (endTime - startTime));
}
}
class PThread_1 implements Runnable {
public static List<Long> lt = new ArrayList<Long>();
public static final Log pThread_1 = LogFactory.getLog(PThread_1.class);
PThread_1() {
}
@Override
public void run() {
// TODO Auto-generated method stub
while (lt.size() < 2048) {
lt.add(System.currentTimeMillis());
}
for (int i = 0; i < 2048 ; i++) {
pThread_1.info(lt.get(i));
}
lt.clear();
}
}
class PThread_2 implements Runnable {
public static List<Long> lt = new ArrayList<Long>();
PThread_2() {
}
@Override
public void run() {