我做了一个线程测试,发现多线程执行时间超过单线程:
测试说明:输出100万个数字。
测试代码如下:
public class ThreadTest { static long nonThreadtime = 0; static long threadStart = 0; public static void main(String args[]) { MyThread11 myThread1 = new MyThread11();
MyThread2 myThread2 = new MyThread2();
long start = System.currentTimeMillis();
for (int i = 0; i < 100000 * 5; i++) {
System.out.println("单线程1:" + i);
}
for (int i = 100000 * 5; i < 1000000; i++) {
System.out.println("单线程2:" + i);
}
System.out.println("单线程" + " ok");
nonThreadtime = System.currentTimeMillis() - start;
threadStart = System.currentTimeMillis();
myThread1.start();
myThread2.start();
} static class MyThread11 extends Thread {
public MyThread11() { }
public void run() {
for (int i = 0; i < 100000 * 5; i++) {
System.out.println("线程1:" + i);
}
setState("run1");
}
}
static class MyThread2 extends Thread {
public void run() {
for (int i = 100000 * 5; i < 1000000; i++) {
System.out.println("线程2:" + i);
}
setState("run2");
}
}
static int threadCount = 0;
public static synchronized void setState(String threadId) {
System.out.println(threadId + " ok");
threadCount++;
if (threadCount == 2) {
System.out.println("非线程耗时" + nonThreadtime);
System.out.println("线程耗时"
+ (System.currentTimeMillis() - threadStart));
}
}
}
执行结果:
非线程耗时15797
线程耗时20828执行很多次都是这样。我是单核的CPU,是否不支持多线程?
测试说明:输出100万个数字。
测试代码如下:
public class ThreadTest { static long nonThreadtime = 0; static long threadStart = 0; public static void main(String args[]) { MyThread11 myThread1 = new MyThread11();
MyThread2 myThread2 = new MyThread2();
long start = System.currentTimeMillis();
for (int i = 0; i < 100000 * 5; i++) {
System.out.println("单线程1:" + i);
}
for (int i = 100000 * 5; i < 1000000; i++) {
System.out.println("单线程2:" + i);
}
System.out.println("单线程" + " ok");
nonThreadtime = System.currentTimeMillis() - start;
threadStart = System.currentTimeMillis();
myThread1.start();
myThread2.start();
} static class MyThread11 extends Thread {
public MyThread11() { }
public void run() {
for (int i = 0; i < 100000 * 5; i++) {
System.out.println("线程1:" + i);
}
setState("run1");
}
}
static class MyThread2 extends Thread {
public void run() {
for (int i = 100000 * 5; i < 1000000; i++) {
System.out.println("线程2:" + i);
}
setState("run2");
}
}
static int threadCount = 0;
public static synchronized void setState(String threadId) {
System.out.println(threadId + " ok");
threadCount++;
if (threadCount == 2) {
System.out.println("非线程耗时" + nonThreadtime);
System.out.println("线程耗时"
+ (System.currentTimeMillis() - threadStart));
}
}
}
执行结果:
非线程耗时15797
线程耗时20828执行很多次都是这样。我是单核的CPU,是否不支持多线程?
解决方案 »
- java自带的类的方法都可以直接使用么?
- JAVA生成XML文件
- JAVA声音报警方案寻求
- 谁哪有PhoneNumberArea.dat源代码
- Jacob写word的问题求助
- 万事万物皆对象,那么 int a=10也是对象吗?
- 弱弱的问题:linux下读取目录信息以及读写文本文件!!!
- 为什么client和server之间的通讯一直不能成功(看原码)在线等待``
- 一个奇怪的问题,但很简单!!!
- UI小问题,可我就是不知道怎么实现,或许您知道……
- 请教:在Windows2003标准版上安装Tomcat连接Oracle9i速度奇慢?
- 面试题:在MVC 模型中,控制层可以控制视图层或者控制层可以控制model层吗?
你的这种测试条件下看不出Thread的好处,看实际的例子,比如word,你写文档的时候有一个线程在后台给你写的东西做语法检测,netbeans/eclipse都可以在你写代码的时候直接发现你写的代码里是否有语法错误,这都是通过多线程来实现的.