当其中一个线程访问object的一个synchronized(this)同步代码块时,其他线程仍然可以访问该object中的非synchronized(this)同步代码块。
测试
package JavaPrograms;
public class MyThreadSynchronization2 implements Runnable{
public void run(){
synchronized(
this){
for(
int i=
5;i>=
1;i--){
System.out.println(Thread.currentThread().getName()+
"使用该电脑"+i+
"次.");
try{
Thread.sleep(
500);
}
catch(InterruptedException e){
}
}
}
for(
int i=
5;i>=
1;i--){
System.out.println(Thread.currentThread().getName()+
"使用该电脑"+i+
"次.");
try{
Thread.sleep(
500);
}
catch(InterruptedException e){
}
}
}
public static void main(String[] args) {
MyThreadSynchronization2 mts=
new MyThreadSynchronization2();
Thread t1=
new Thread(mts,
"学生甲");
Thread t2=
new Thread(mts,
"学生乙");
t1.start();
t2.start();
}
}
转载请注明原文地址: https://ju.6miu.com/read-679927.html