Posted by: didiksoft | September 22, 2011

The Power of SingleExecutor

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.