[concurrency-interest] overview new features -> tim.

Peter Veentjer - Anchor Men p.veentjer at anchormen.nl
Tue Sep 6 03:26:25 EDT 2005


Hi Tim,

Your solution looks a lot like mine. I have create the
Input/OutputChannel
Instead of the Puttable/Takable. Although I find puttable and takable
Easier to understand (you can put things in a output channel, and
retrieve
Things from a input channen, feels Strange at first) but they are well
known 
names. So I didn`t want to create new names.

The PTAdapters are nice. I have created something similar:
The BufferedChannel.

Example:
Channel c = new BufferedChannel(new BlockingQueue());
InputChannel in = c;
OutputChannel out = c;

And the threading part... If have been strugling with it in the
Beginning, but I have found the following solutions:

1---------------------------------------
I can make channels active:
OutputChannel c = new DoSomeHeavyCalculatingOutputChannel(..);
ActiveOutputChannel a = new ActiveOutputChannel(c,someExecutorService);

If a message is put/offered on a, a new runnable is put on the 
someExecutorService and the thread that put it there isn`t
Used for putting the message on c, but a thread from the
'someExecutorService' is used.

2---------------------------------------
But also have created something new recently: the Repeater. 
Example:

Channel fetchChannel = new StdBufferedChannel();
Channel protocolChan = new StdBufferedChannel();
Channel parseDataChan = new StdBufferedChannel();

RepeaterService fetchRepeater = new StdRepeaterService(40);//40 threads
fetchRepeater.start();
BetterFetcher fetcher = new
BetterFetcher(fetchChannel,protocolChan,fetchRepeater);
fetcher.start();

RepeaterService analyseRepeater = new StdRepeaterService();
analyseRepeater.start();
Analyzer analyzer = new
Analyzer(protocolChan,parseDataChan,analyseRepeater);
analyzer.start();

RepeaterService downManRepeater = new StdRepeaterService();
downManRepeater.start();
DownloadManager downMan = new
DownloadManager(parseDataChan,fetchChannel,downManRepeater);
downMan.start();

In this example there are 3 channels:
fetchChannel (you can put requests in here for pages to download)
protocolChan (the fetcher puts the downloaded pages in here)
parseDataChan (the analyzer put the analyzed pages (with outlinks) in
here))

The Repeaters can be compared to a threadpool, but they keep
Executing the same task over and over again. In this example
The repeaters keep sucking on channels to recieve messages
And process them.

This is the code of the BetterFetcher:

public class BetterFetcher {

	private InputChannel _channel;
	private Repeater _repeater;
	private Channel _protocolOutputChannel;

	public BetterFetcher(InputChannel channel, Channel
protocolOutputChannel, Repeater repeater) {
		if (channel ==
null||protocolOutputChannel==null||repeater == null) throw new
NullPointerException();
		_channel = channel;
		_repeater = repeater;
		_protocolOutputChannel = protocolOutputChannel;
	}

	public void start() throws InterruptedException {
		_repeater.repeat(new RunnableImpl());
	}

	private class RunnableImpl implements Runnable{

		private void runInterrupteble()throws
InterruptedException{
			FetchListEntry fle =
(FetchListEntry)_channel.take();
			String url = fle.getPage().getURL().toString();
			System.out.println("fetching url: "+url);
			Protocol protocol = null;
			try {
				try{
					protocol =
ProtocolFactory.getProtocol(url);
				}catch(RuntimeException ex){
					ex.printStackTrace();
					throw ex;
				}

				ProtocolOutput output =
protocol.getProtocolOutput(fle);
				System.out.println("page succesfully
retrieved");
				_protocolOutputChannel.put(new
X(fle,output));
			} catch (ProtocolNotFound protocolNotFound) {
				System.out.println("no protocol found");
				protocolNotFound.printStackTrace();
			}
		}

		public void run() {
			try {
				runInterrupteble();
			} catch (InterruptedException e) {
				e.printStackTrace();  //To change body
of catch statement use File | Settings | File Templates.
			}
		}
	}
}


At the moment I`m experimenting with the repeaters in a prototype, 
But the ActiveChannels have been used in a couple of projects and 
I`m totally in love with this channeled approach. 






More information about the Concurrency-interest mailing list