如下java代码:package com.test;public class ThreadTest implements Runnable { private static int i = 0;
private static int j = 0; public void run() {
f1();
f2();
} private void f1() {
synchronized (ThreadTest.class) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("currentThread:"
+ Thread.currentThread().getId());
System.out.println("i:" + (++i) + "currentThread:"
+ Thread.currentThread().getId());
} } private void f2() {
synchronized (ThreadTest .class) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\tcurrentThread:"
+ Thread.currentThread().getId());
System.out.println("j:" + (++j) + "\tcurrentThread:"
+ Thread.currentThread().getId());
} } public static void main(String[] args) { Thread th1 = new Thread(new ThreadTest ());
Thread th2 = new Thread(new ThreadTest ());
Thread th3 = new Thread(new ThreadTest ());
Thread th4 = new Thread(new ThreadTest ());
th1.start();
th2.start();
th3.start();
th4.start(); }
}
为什么f1()和f2()的第二行打印是同步的,而第一行却不同步?注释掉第一行,打印结果如下:
i:1 currentThread:8
j:1 currentThread:8
i:2 currentThread:11
j:2 currentThread:11
i:3 currentThread:9
j:3 currentThread:9
i:4 currentThread:10
j:4 currentThread:10注释掉第二行,打印结果如下:
currentThread:8
currentThread:11
currentThread:11
currentThread:9
currentThread:9
currentThread:10
currentThread:8
currentThread:10
private static int j = 0; public void run() {
f1();
f2();
} private void f1() {
synchronized (ThreadTest.class) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("currentThread:"
+ Thread.currentThread().getId());
System.out.println("i:" + (++i) + "currentThread:"
+ Thread.currentThread().getId());
} } private void f2() {
synchronized (ThreadTest .class) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\tcurrentThread:"
+ Thread.currentThread().getId());
System.out.println("j:" + (++j) + "\tcurrentThread:"
+ Thread.currentThread().getId());
} } public static void main(String[] args) { Thread th1 = new Thread(new ThreadTest ());
Thread th2 = new Thread(new ThreadTest ());
Thread th3 = new Thread(new ThreadTest ());
Thread th4 = new Thread(new ThreadTest ());
th1.start();
th2.start();
th3.start();
th4.start(); }
}
为什么f1()和f2()的第二行打印是同步的,而第一行却不同步?注释掉第一行,打印结果如下:
i:1 currentThread:8
j:1 currentThread:8
i:2 currentThread:11
j:2 currentThread:11
i:3 currentThread:9
j:3 currentThread:9
i:4 currentThread:10
j:4 currentThread:10注释掉第二行,打印结果如下:
currentThread:8
currentThread:11
currentThread:11
currentThread:9
currentThread:9
currentThread:10
currentThread:8
currentThread:10
解决方案 »
- sql2008 初学者 用JAVA连接不上
- 请教一个简单的问题(让程序暂停执行100ms)
- jre 相关
- java 包的一个概念问题,请教
- Jsp与Servlet的小问题?
- 在一台机器上安装JDK1.4和JDK1.5,并在这台机器上可以同时运行基于这两个JDK编译的不同程序??
- 怎么在KeyListener里过滤掉字符,只留下数字
- 请问 class.newInstance() 和 class _class = new class() 有什莫区别吗?
- java or oracle 技术群
- 我这小程序中怎么总有一个date的错误?请大家帮我看看。50分
- findbug java clone 问题
- java初级 关于构造函数
synchronized(ThreadTest.class) {
f1();
f2();
}
} 就会f1()和f2()就会顺序执行了