多线程环境下的static静态方法跟类方法
多线程环境下的static静态方法和类方法
今天无意之中看到一篇讨论静态方法和类方法的帖子。于是就想着测一测,遇到了部分问题。希望有大神能给解答一下
我的java代码:
包:isStatic
Method.java
UseLittlelong.java
pro.properties
我在本机上(i5-4300U 1.9GHz 8G)跑出来的结果为:
我在服务器上(Intel(R) Xeon(R) CPU E5520 @ 2.27GHz 48G)跑多次出来的结果为:
今天无意之中看到一篇讨论静态方法和类方法的帖子。于是就想着测一测,遇到了部分问题。希望有大神能给解答一下
我的java代码:
包:isStatic
Method.java
package isStatic;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class Method {
public static void littlelong1(String name) {
long i = 0;
while (i ++ != 100000000l) {
//do nothing , but not sleep
}
}
public void littlelong2(String name) {
long i = 0;
while (i ++ != 100000000l) {
//do nothing , but not sleep
}
}
/*这是为了我测试方便,可以删掉的*/
public static int getConfig() {
Properties p = new Properties();
try {
p.load(new FileReader("pro.properties"));
} catch (IOException e) {
e.printStackTrace();
}
return Integer.parseInt(p.getProperty("LENGTH"));
}
}
UseLittlelong.java
package isStatic;
public class UseLittlelong {
private static int LENGTH = 20;
private int[] avg_static = new int[LENGTH];
private int[] avg_nonstatic = new int[LENGTH];
//divided
private Method m = new Method();
public static void main(String[] args) {
UseLittlelong ull = new UseLittlelong();
ull.staticTest();
ull.nonstaticTest();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ull.avg();
}
/*test static*/
public void staticTest() {
int i = -1 ;
/*原谅我选择了再循环中启动线程,只是这样显得更方便一些*/
while (i ++ < LENGTH - 1) {
new Use(true,i).start();
}
}
/*test non-static*/
public void nonstaticTest() {
int i = -1 ;
while (i ++ < LENGTH - 1) {
new Use(false,i).start();
}
}
/*average count*/
public void avg() {
long sta = 0,nonsta = 0;
for (int i = 0 ; i < LENGTH; i++) {
sta += avg_static[i];
nonsta += avg_nonstatic[i];
}
System.out.println("static avg :" + sta / LENGTH);
System.out.println("non-static avg :" + nonsta / LENGTH);
}
/**
* @author qyp199312
*/
class Use extends Thread {
private boolean isStatic;
private String threadName;
private int threadId;
public Use(boolean b,int threadId) {
this.isStatic = b;
this.threadId = threadId;
this.threadName = (b ? "[static-" : "[nonstatic-")+ threadId +"] ";
}
public void run () {
if (isStatic) {
long begin = System.currentTimeMillis();
Method.littlelong1(threadName);
avg_static[threadId] = (int) (System.currentTimeMillis() - begin);
// System.out.println(threadName + avg_static[threadId] );
} else {
long begin = System.currentTimeMillis();
// new Method().littlelong2(threadName);
m.littlelong2(threadName);
avg_nonstatic[threadId] = (int) (System.currentTimeMillis() - begin);
// System.out.println(threadName + avg_nonstatic[threadId] );
}
}
}
}
pro.properties
#可在源码中置换为实际数字
LENGTH = 20
我在本机上(i5-4300U 1.9GHz 8G)跑出来的结果为:
static avg :602
non-static avg :280
我在服务器上(Intel(R) Xeon(R) CPU E5520 @ 2.27GHz 48G)跑多次出来的结果为:
[tt ~]$ java isStatic/UseLittlelong
static avg :438
non-static avg :505
[tt ~]$ java isStatic/UseLittlelong
static avg :439
non-static avg :480
[tt ~]$ java isStatic/UseLittlelong
static avg :486
non-static avg :526
[tt ~]$ java isStatic/UseLittlelong
static avg :337
non-static avg :448
[tt ~]$ java isStatic/UseLittlelong
static avg :460
non-static avg :517
[tt ~]$ java isStatic/UseLittlelong
static avg :369
non-static avg :466
[tt ~]$ java isStatic/UseLittlelong
static avg :402
non-static avg :441
[tt ~]$ java isStatic/UseLittlelong
static avg :462
non-static avg :498
[tt ~]$ java isStatic/UseLittlelong