一个例子就是生产消费者问题,
生产出来了后就通知消费者,消费没有了就堵住
看程序吧,这段有操作系统理论课的人回觉得简单,不然要找书看看
package mod13;
public class Producer implements Runnable
{
SyncStack theStack; public Producer(SyncStack s) {
theStack = s;
}
public void run() {
char c;
for (int i = 0; i < 20; i++) {
c = (char)(Math.random() * 26 + 'A');
theStack.push(c);
System.out.println("Produced: " + c);
try {
Thread.sleep((int)(Math.random() * 100));
} catch (InterruptedException e) {
// ignore it..
}
}
}
} package mod13;
public class SyncTest
{
public static void main(String args[]) {
SyncStack stack = new SyncStack();
Runnable source = new Producer(stack);
Runnable sink = new Consumer(stack);
Thread t1 = new Thread(source);
Thread t2 = new Thread(sink);
t1.start();
t2.start();
}
}
package mod13;
public class SyncStack
{
private int index = 0;
private char [] buffer = new char[6]; public synchronized char pop() {
while (index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
// ignore it..
}
}
this.notify();
index--;
return buffer[index];
} public synchronized void push(char c) {
while (index == buffer.length) {
try {
this.wait();
} catch (InterruptedException e) {
// ignore it..
}
}
this.notify();
buffer[index] = c;
index++;
}
}
package mod13;
public class Consumer implements Runnable
{
SyncStack theStack; public Consumer(SyncStack s) {
theStack = s;
} public void run() {
char c;
for (int i = 0; i < 20; i++) {
c = theStack.pop();
System.out.println("
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
// ignore it..
}
}
}
}
生产出来了后就通知消费者,消费没有了就堵住
看程序吧,这段有操作系统理论课的人回觉得简单,不然要找书看看
package mod13;
public class Producer implements Runnable
{
SyncStack theStack; public Producer(SyncStack s) {
theStack = s;
}
public void run() {
char c;
for (int i = 0; i < 20; i++) {
c = (char)(Math.random() * 26 + 'A');
theStack.push(c);
System.out.println("Produced: " + c);
try {
Thread.sleep((int)(Math.random() * 100));
} catch (InterruptedException e) {
// ignore it..
}
}
}
} package mod13;
public class SyncTest
{
public static void main(String args[]) {
SyncStack stack = new SyncStack();
Runnable source = new Producer(stack);
Runnable sink = new Consumer(stack);
Thread t1 = new Thread(source);
Thread t2 = new Thread(sink);
t1.start();
t2.start();
}
}
package mod13;
public class SyncStack
{
private int index = 0;
private char [] buffer = new char[6]; public synchronized char pop() {
while (index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
// ignore it..
}
}
this.notify();
index--;
return buffer[index];
} public synchronized void push(char c) {
while (index == buffer.length) {
try {
this.wait();
} catch (InterruptedException e) {
// ignore it..
}
}
this.notify();
buffer[index] = c;
index++;
}
}
package mod13;
public class Consumer implements Runnable
{
SyncStack theStack; public Consumer(SyncStack s) {
theStack = s;
} public void run() {
char c;
for (int i = 0; i < 20; i++) {
c = theStack.pop();
System.out.println("
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
// ignore it..
}
}
}
}
判断条件很简单,见pop()和push().要实现你的问题,只需要在SynStack里设置一个boolean值,boolean begin =true;
pop() 和push()中判断条件更改为
pop()
while (begin) {
try {
wait();
} catch (InterruptedException e) {
// ignore it..
}
}
do...
notify();
begin =true;
}
}
2.push()
while (!begin) {
try {
wait();
} catch (InterruptedException e) {
// ignore it..
}
}
do...
notify();
begin =false;
}
}