62,614
社区成员
发帖
与我相关
我的任务
分享
class MyThread implements Runnable{
static int i=0;
public void run() {
while(i<10000){
i++;
}
}
}
public class ThreadTest {
public static void test1() {
long start = System.currentTimeMillis();
int j = 0;
while (j <10000) {
j++;
}
long end = System.currentTimeMillis();
System.out.println(j);
System.out.println("所用时间为" + (end - start) + "ms");
}
public static void test2() {
long start = System.currentTimeMillis();
new Thread(new MyThread()).start();
new Thread(new MyThread()).start();
new Thread(new MyThread()).start();
new Thread(new MyThread()).start();
System.out.println(MyThread.i);
long end = System.currentTimeMillis();
System.out.println("所用时间为:" + ((end - start)) + "ms");
}
public static void main(String[] args) {
test1();
test2();
}
}
class MyThread implements Runnable{
static int i=0;
public void run() {
synchronized (MyThread.class) {
while(i<10000){
i++;
}
}
}
}