public void push(char c) {
synchronized(this) {
....
}
}
public synchronized void push(char c) {
....
}
Return to top /ul>
public class SyncStack {
private Vector buffer = new Vector(400, 200);
//communication of two
public synchronized char pop() {
char c;
while (buffer.size() == 0) {
try {
this.wait();
} catch (InterruptedException e) {
// ignore it...
}
}
c = ((Character)buffer.remove(buffer.size()-1)).charValue();
return c;
}
public synchronized void push(char c) {
this.notify();
Character charObj = new Character(c);
buffer.addElement(charObj);
}
public class ControlledThread extends Thread {
static final int SUSP = 1;
static final int STOP = 2;
static final int RUN = 0;
private int state = RUN;
public synchronized void setState(int s) {
state = s;
if ( s == RUN ) {
notify();
}
}
public synchronized boolean checkState() {
while ( state == SUSP ) {
try {
wait();
} catch (InterruptedException e) {
// ignore
}
}
if ( state == STOP ) {
return false;
}
return true;
}
public void run() {
while ( true ) {
// doSomething();
// Be sure shared data is in consistent state in
// case the thread is waited or marked for exiting
// from run()
if ( !checkState() ) {
break;
}
}
}
}