package basic;
public class TestSynchronizedObject {
public static void main(String[] args) {
final MyObject myObject_first = new MyObject();
final MyObject myObject_seconde = new MyObject();
new Thread("线程A") {
@Override
public void run() {
myObject_first.print("线程A");
}
}.start();
new Thread("线程B") {
@Override
public void run() {
myObject_seconde.print("线程B");
}
}.start();
}
}
class MyObject {
public void print(String str) {
System.out.println("线程" + Thread.currentThread().getName() + "开始执行");
synchronized (String.class) {
for (int i = 0; i < 10; i++) {
System.out.println(str + " ." + i + ". ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
System.out.println("线程" + Thread.currentThread().getName() + "执行结束");
}
}
输出结果:
线程线程A开始执行
线程A .0.
线程线程B开始执行
线程A .1.
线程A .2.
线程A .3.
线程A .4.
线程A .5.
线程A .6.
线程A .7.
线程A .8.
线程A .9.
线程线程A执行结束
线程B .0.
线程B .1.
线程B .2.
线程B .3.
线程B .4.
线程B .5.
线程B .6.
线程B .7.
线程B .8.
线程B .9.
线程线程B执行结束
很显然,两个对象互斥执行了。可视我锁住的是String的所有实例化对象,而我开的线程用的是MyObject对象,我既然没有锁住MyObject对象,它为什么互斥执行呢?
package basic;
public class TestSynchronizedObject {
public static void main(String[] args) {
final MyObject myObject_first = new MyObject();
final MyObject myObject_seconde = new MyObject();
new Thread("线程A") {
@Override
public void run() {
myObject_first.print("线程A");
}
}.start();
new Thread("线程B") {
@Override
public void run() {
myObject_seconde.print("线程B");
}
}.start();
}
}
class MyObject {
public void print(String str) {
System.out.println("线程" + Thread.currentThread().getName() + "开始执行");
synchronized (str) {
for (int i = 0; i < 10; i++) {
System.out.println(str + " ." + i + ". ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
System.out.println("线程" + Thread.currentThread().getName() + "执行结束");
}
}
如果这样,它的输出结果为:
线程线程A开始执行
线程A .0.
线程线程B开始执行
线程A .1.
线程A .2.
线程A .3.
线程A .4.
线程A .5.
线程A .6.
线程A .7.
线程A .8.
线程A .9.
线程B .0.
线程线程A执行结束
线程B .1.
线程B .2.
线程B .3.
线程B .4.
线程B .5.
线程B .6.
线程B .7.
线程B .8.
线程B .9.
线程线程B执行结束
关于synchronized修饰代码块时,括号后面要跟什么,有没有这方面的博客详细解答一下哈,谢谢~~
可以参考我的博客
线程的同步之Synchronized的使用
线程的同步之Synchronized在单例模式中的应用