1. 有关线程的一道题
package tengteng.org;
public class SynchronizedTest implements Runnable{
int b = 100;
public synchronized void m1(){
b = 1000;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m1中的b="+b);
}
public void m2(){
System.out.println("m2中的b="+b);
}
public void run() {
m1();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedTest syt = new SynchronizedTest();
Thread tr = new Thread(syt);
tr.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
syt.m2();
}
}
运行出来结果是什么呢?
m2中的b=1000
m1中的b=1000
很吃惊吧,不是已经被锁定了吗,怎么还可以访问。也许是一个BUG,不是!
记住:在synchronized语句块中的整体不能被访问,而m2可以被访问。(语句块中的修改能被访问到)。
package tengteng.org;
public class SynchronizedTest implements Runnable{
int b = 100;
public synchronized void m1(){
b = 1000;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m1中的b="+b);
}
public void m2(){
b = 2000;
}
public void run() {
m1();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedTest syt = new SynchronizedTest();
Thread tr = new Thread(syt);
tr.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
syt.m2();
}
}
运行结果:m1中的b=2000
说明:synchronized并不是一个完全锁,用同步的时候一定要认真考虑,仔细调试。
int b = 100;
public synchronized void m1(){
b = 1000;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m1中的b="+b);
}
public synchronized void m2(){
b = 2000;
}
public void run() {
m1();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedTest syt = new SynchronizedTest();
Thread tr = new Thread(syt);
tr.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
syt.m2();
System.out.println("在方法的最后b:"+b);
}
输出为:
m1中的b=1000
在方法的最后b:2000
在方法中用同步并不是想象的那么简单。
#############################################################
int b = 100;
public synchronized void m1(){
b = 1000;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m1中的b="+b);
System.out.println("这里执行了m1");
}
public synchronized void m2(){
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
}
b = 2000;
System.out.println("这里执行了m2");
}
public void run() {
m1();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedTest syt = new SynchronizedTest();
Thread tr = new Thread(syt);
tr.start();
syt.m2();
System.out.println("在方法的最后b:"+b);
}
输出:
这里执行了m2
在方法的最后b:2000
m1中的b=1000
这里执行了m1
int b = 100;
public synchronized void m1(){
b = 1000;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m1中的b="+b);
System.out.println("这里执行了m1");
}
public synchronized void m2(){
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
}
b = 2000;
System.out.println("这里执行了m2");
}
public void run() {
m1();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedTest syt = new SynchronizedTest();
Thread tr = new Thread(syt);
tr.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
syt.m2();
System.out.println("在方法的最后b:"+b);
}
输出:
m1中的b=1000
这里执行了m1
这里执行了m2
在方法的最后b:2000