我做了一个线程测试,发现多线程执行时间超过单线程:
测试说明:输出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,是否不支持多线程?
解决方案 »
- [color=#FF0000]急:将Jlabel分成两段[/color]
- 请教txt文件分页的问题?
- 再发:用java的applet调用delphi封装第三方动态库函数的exe文件出错,急!!!!!分不够再加
- 菜鸟求助,RandomAccessFile的问题
- 初学JAVA,helloworld也显示不出来.请哥哥姐姐大虾小虾帮忙!在线等
- 我不认为JAVA有什么好的优点
- 能否设计这么一个java程序?
- 请教一个问题
- 请教运算符问题
- 新人求告诉为什么在html编写代码页面引用不成JQuery。
- 请教:在Windows2003标准版上安装Tomcat连接Oracle9i速度奇慢?
- 面试题:在MVC 模型中,控制层可以控制视图层或者控制层可以控制model层吗?
你的这种测试条件下看不出Thread的好处,看实际的例子,比如word,你写文档的时候有一个线程在后台给你写的东西做语法检测,netbeans/eclipse都可以在你写代码的时候直接发现你写的代码里是否有语法错误,这都是通过多线程来实现的.