看看这个consumer/producer的多线程互斥的例子吧。
//ProducerConsumerTest.java
public class ProducerConsumerTest {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1); p1.start();
c1.start();
}
}//CubbyHole.java
public class CubbyHole {
private int contents;
private boolean available = false; public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
//notifyAll();
notify();
return contents;
} public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
//notifyAll();
notify();
}
}//Producer.java
public class Producer extends Thread {
private CubbyHole cubbyhole;
private int number; public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
} public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number
+ " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
//Consumer.java
public class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number; public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
} public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
System.out.println("Consumer #" + this.number
+ " got: " + value);
}
}
}
//ProducerConsumerTest.java
public class ProducerConsumerTest {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1); p1.start();
c1.start();
}
}//CubbyHole.java
public class CubbyHole {
private int contents;
private boolean available = false; public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
//notifyAll();
notify();
return contents;
} public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
//notifyAll();
notify();
}
}//Producer.java
public class Producer extends Thread {
private CubbyHole cubbyhole;
private int number; public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
} public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number
+ " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
//Consumer.java
public class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number; public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
} public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
System.out.println("Consumer #" + this.number
+ " got: " + value);
}
}
}
public Object lock = new Object();methodA() {
somenonsyncode();
synchronized (lock) {
}
}methodB(){
somenonsyncode();
synchronized (lock) {
}
}这样两个方法就能够使到两个地方互斥了
但是这个共享只能针对类共享,换句话说如果你想实现 主类型(如 int)共享使用就必须把主类型封装到类里面。
在java里面每一个实例后的的类都会有一个特殊的标志(由java虚拟机控制)
如果你使用了这个实例的带有synchronized关键字的方法,那么java虚拟机把标志变成占用
如果这时候你又要使用这个实例的带有synchronized关键字的其他方法的时候,虚拟机发现标志已经被占用,那么就会等待。一直等待到标志被放开以后。就去执行.如果有疑问,请看看think-in-java
也可以写出来,我们讨论一下!祝你好运!