have you work with synchronize wait and notify? here it goes
class Queue extends thread{
private Vector<String> v=new Vector<String>();
public void add(String data){
synchronize(v){
v.addElement(data);
v.notify();
}
}
public void run(){
while(true){
String data=v.remove(0);
if(data==null){
synchronize(v){
v.wait();
}
}else{
//do something
}
}
}
}
that class functionality can be replaced with Executors.newSingleExecutor(); , and no wait notify anymore:)
Advertisement
Recent Comments