java中类中多个Synchronized方法
类中多个Synchronized方法
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
下面给出一个例子,说明一个class中有两个方法synchronized的情况。它们互相阻挡的用法和上面的“一个方法有synchronized”的情况是一样的。
例1.9.5:
class A {
public synchronized void f1() {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("f1 i = " + i);
}
}
public synchronized void f2() {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("f2 i = " + i);
}
}
}
class MyThread1 extends Thread {
A a;
public MyThread1(A a) {
this.a = a;
}
public void run() {
a.f1();
}
}
class MyThread2 extends Thread {
A a;
public MyThread2(A a) {
this.a = a;
}
public void run() {
a.f2();
}
}
public class TestMark_to_win {
public static void main(String[] args) {
A a = new A();
Thread t1 = new MyThread1(a);
Thread t2 = new MyThread2(a);
t1.start();
t2.start();
}
}
输出结果:
f1 i = 0
f1 i = 1
f1 i = 2
f2 i = 0
f2 i = 1
f2 i = 2
上面两个synchronized的方法如其中一个无限循环,一定会挡住其他的方法。下面的例子先进f1的线程死死挡住想进f2的线程。
例1.9.5_b:
class A {
public synchronized void f1() {
//快速无限循环,别的线程什么也做不了
while (true) {
}
}
public synchronized void f2() {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("f2 i = " + i);
}
}
}
class MyThread1 extends Thread {
A a;
public MyThread1(A a) {
this.a = a;
}
public void run() {
a.f1();
}
}
class MyThread2 extends Thread {
A a;
public MyThread2(A a) {
this.a = a;
}
public void run() {
a.f2();
}
}
public class TestMark_to_win {
public static void main(String[] args) {
A a = new A();
Thread t1 = new MyThread1(a);
Thread t2 = new MyThread2(a);
t1.start();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
t2.start();
}
}
结果什么也输不出来
如果我们想达到这个目的:想用f1的线程互相挡住,而想用f1和f2的不同线程之间互相不挡,我们得用不同的锁。
例1.9.5_c:
class A {
private Object lock1 = new Object();
private Object lock2 = new Object();
public void f1() {
synchronized (lock1) {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在无限耗用你的CPU,eclipse的红灯还在亮");
}
}
}
public void f2() {
synchronized (lock2) {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("f2 i = " + i
+ Thread.currentThread().getName());
}
}
}
}
class MyThread1 extends Thread {
A a;
public MyThread1(A a) {
this.a = a;
}
public void run() {
a.f1();
}
}
class MyThread2 extends Thread {
A a;
public MyThread2(A a) {
this.a = a;
}
public void run() {
a.f2();
}
}
public class Test {
public static void main(String[] args) {
A a = new A();
Thread t1 = new MyThread1(a);
Thread t11 = new MyThread1(a);
Thread t2 = new MyThread2(a);
t1.start();
t11.start();
t2.start();
}
}
输出结果:
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮
f2 i = 0Thread-2
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮
f2 i = 1Thread-2
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮
f2 i = 2Thread-2
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮
Thread-1正在无限耗用你的CPU,eclipse的红灯还在亮