下面是一个小例子,看看不同的持有对象对共享对象操作的影响,结果自己跑一下看看。B是期望结果。 //Test.java public class Test { public static void main(String[] args) throws InterruptedException { A a1 = new A(); A a2 = new A(5, 200); new Thread(a1).start(); new Thread(a2).start(); Thread.sleep(2000); System.out.println("================================================"); B b1 = new B(); B b2 = new B(5, 200); new Thread(b1).start(); new Thread(b2).start(); } }class A implements Runnable { public static int shareObj = 0; private int times = 5; private long sleep = 100; A() {} A(int times, long sleep) { this.times = times; this.sleep = sleep; } public void run() { for (int i = 0; i < times; i++) { synchronized (this) { System.out.println(Thread.currentThread() + " before executing: " + shareObj); try { Thread.sleep(sleep); } catch (InterruptedException ex) { } shareObj++; System.out.println(Thread.currentThread() + " after executing: " + shareObj); } } } }class B implements Runnable { public static Object locker = new Object(); public static int shareObj = 0; private int times = 5; private long sleep = 100; B() {} B(int times, long sleep) { this.times = times; this.sleep = sleep; } public void run() { for (int i = 0; i < times; i++) { synchronized (locker) { System.out.println(Thread.currentThread() + " before executing: " + shareObj); try { Thread.sleep(sleep); } catch (InterruptedException ex) { } shareObj++; System.out.println(Thread.currentThread() + " after executing: " + shareObj); } } } }
private static Object dummyLock = new Object();
这样才能保证语句块的同步;使用this则不行了,?因为出现了多个A Thread实例,而每个实例中this指向自己,...自己领会吧
//Test.java
public class Test {
public static void main(String[] args) throws InterruptedException {
A a1 = new A();
A a2 = new A(5, 200);
new Thread(a1).start();
new Thread(a2).start();
Thread.sleep(2000);
System.out.println("================================================");
B b1 = new B();
B b2 = new B(5, 200);
new Thread(b1).start();
new Thread(b2).start(); }
}class A implements Runnable { public static int shareObj = 0; private int times = 5;
private long sleep = 100; A() {} A(int times, long sleep) {
this.times = times;
this.sleep = sleep;
} public void run() {
for (int i = 0; i < times; i++) {
synchronized (this) {
System.out.println(Thread.currentThread() + " before executing: " + shareObj);
try {
Thread.sleep(sleep);
}
catch (InterruptedException ex) {
} shareObj++; System.out.println(Thread.currentThread() + " after executing: " + shareObj);
}
}
}
}class B implements Runnable { public static Object locker = new Object();
public static int shareObj = 0; private int times = 5;
private long sleep = 100; B() {} B(int times, long sleep) {
this.times = times;
this.sleep = sleep;
} public void run() {
for (int i = 0; i < times; i++) {
synchronized (locker) {
System.out.println(Thread.currentThread() + " before executing: " + shareObj);
try {
Thread.sleep(sleep);
}
catch (InterruptedException ex) {
} shareObj++; System.out.println(Thread.currentThread() + " after executing: " + shareObj);
}
}
}
}