Java Doc for Channel.java in  » Database-JDBC-Connection-Pool » proxool » org » logicalcobwebs » concurrent » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » proxool » org.logicalcobwebs.concurrent 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


org.logicalcobwebs.concurrent.Channel

All known Subclasses:   org.logicalcobwebs.concurrent.LinkedQueue,
Channel
public interface Channel extends Puttable,Takable(Code)
Main interface for buffers, queues, pipes, conduits, etc.

A Channel represents anything that you can put items into and take them out of. As with the Sync interface, both blocking (put(x), take), and timeouts (offer(x, msecs), poll(msecs)) policies are provided. Using a zero timeout for offer and poll results in a pure balking policy.

To aid in efforts to use Channels in a more typesafe manner, this interface extends Puttable and Takable. You can restrict arguments of instance variables to this type as a way of guaranteeing that producers never try to take, or consumers put. for example:

 class Producer implements Runnable {
 final Puttable chan;
 Producer(Puttable channel) { chan = channel; }
 public void run() {
 try {
 for(;;) { chan.put(produce()); }
 }
 catch (InterruptedException ex) {}
 }
 Object produce() { ... }
 }
 class Consumer implements Runnable {
 final Takable chan;
 Consumer(Takable channel) { chan = channel; }
 public void run() {
 try {
 for(;;) { consume(chan.take()); }
 }
 catch (InterruptedException ex) {}
 }
 void consume(Object x) { ... }
 }
 class Setup {
 void main() {
 Channel chan = new SomeChannelImplementation();
 Producer p = new Producer(chan);
 Consumer c = new Consumer(chan);
 new Thread(p).start();
 new Thread(c).start();
 }
 }
 

A given channel implementation might or might not have bounded capacity or other insertion constraints, so in general, you cannot tell if a given put will block. However, Channels that are designed to have an element capacity (and so always block when full) should implement the BoundedChannel subinterface.

Channels may hold any kind of item. However, insertion of null is not in general supported. Implementations may (all currently do) throw IllegalArgumentExceptions upon attempts to insert null.

By design, the Channel interface does not support any methods to determine the current number of elements being held in the channel. This decision reflects the fact that in concurrent programming, such methods are so rarely useful that including them invites misuse; at best they could provide a snapshot of current state, that could change immediately after being reported. It is better practice to instead use poll and offer to try to take and put elements without blocking. For example, to empty out the current contents of a channel, you could write:

 try {
 for (;;) {
 Object item = channel.poll(0);
 if (item != null)
 process(item);
 else
 break;
 }
 }
 catch(InterruptedException ex) { ... }
 

However, it is possible to determine whether an item exists in a Channel via peek, which returns but does NOT remove the next item that can be taken (or null if there is no such item). The peek operation has a limited range of applicability, and must be used with care. Unless it is known that a given thread is the only possible consumer of a channel, and that no time-out-based offer operations are ever invoked, there is no guarantee that the item returned by peek will be available for a subsequent take.

When appropriate, you can define an isEmpty method to return whether peek returns null.

Also, as a compromise, even though it does not appear in interface, implementation classes that can readily compute the number of elements support a size() method. This allows careful use, for example in queue length monitors, appropriate to the particular implementation constraints and properties.

All channels allow multiple producers and/or consumers. They do not support any kind of close method to shut down operation or indicate completion of particular producer or consumer threads. If you need to signal completion, one way to do it is to create a class such as

 class EndOfStream {
 // Application-dependent field/methods
 }
 
And to have producers put an instance of this class into the channel when they are done. The consumer side can then check this via
 Object x = aChannel.take();
 if (x instanceof EndOfStream)
 // special actions; perhaps terminate
 else
 // process normally
 

In time-out based methods (poll(msecs) and offer(x, msecs), time bounds are interpreted in a coarse-grained, best-effort fashion. Since there is no way in Java to escape out of a wait for a synchronized method/block, time bounds can sometimes be exceeded when there is a lot contention for the channel. Additionally, some Channel semantics entail a ``point of no return'' where, once some parts of the operation have completed, others must follow, regardless of time bound.

Interruptions are in general handled as early as possible in all methods. Normally, InterruptionExceptions are thrown in put/take and offer(msec)/poll(msec) if interruption is detected upon entry to the method, as well as in any later context surrounding waits.

If a put returns normally, an offer returns true, or a put or poll returns non-null, the operation completed successfully. In all other cases, the operation fails cleanly -- the element is not put or taken.

As with Sync classes, spinloops are not directly supported, are not particularly recommended for routine use, but are not hard to construct. For example, here is an exponential backoff version:

 Object backOffTake(Channel q) throws InterruptedException {
 long waitTime = 0;
 for (;;) {
 Object x = q.poll(0);
 if (x != null)
 return x;
 else {
 Thread.sleep(waitTime);
 waitTime = 3 * waitTime / 2 + 1;
 }
 }
 

Sample Usage. Here is a producer/consumer design where the channel is used to hold Runnable commands representing background tasks.

 class Service {
 private final Channel channel = ... some Channel implementation;
 private void backgroundTask(int taskParam) { ... }
 public void action(final int arg) {
 Runnable command =
 new Runnable() {
 public void run() { backgroundTask(arg); }
 };
 try { channel.put(command) }
 catch (InterruptedException ex) {
 Thread.currentThread().interrupt(); // ignore but propagate
 }
 }
 public Service() {
 Runnable backgroundLoop =
 new Runnable() {
 public void run() {
 for (;;) {
 try {
 Runnable task = (Runnable)(channel.take());
 task.run();
 }
 catch (InterruptedException ex) { return; }
 }
 }
 };
 new Thread(backgroundLoop).start();
 }
 }
 

[ Introduction to this package. ]
See Also:   Sync
See Also:   BoundedChannel





Method Summary
public  booleanoffer(Object item, long msecs)
     Place item in channel only if it can be accepted within msecs milliseconds.
public  Objectpeek()
     Return, but do not remove object at head of Channel, or null if it is empty.
public  Objectpoll(long msecs)
     Return and remove an item from channel only if one is available within msecs milliseconds.
public  voidput(Object item)
     Place item in the channel, possibly waiting indefinitely until it can be accepted.
public  Objecttake()
     Return and remove an item from channel, possibly waiting indefinitely until such an item exists. some item from the channel.



Method Detail
offer
public boolean offer(Object item, long msecs) throws InterruptedException(Code)
Place item in channel only if it can be accepted within msecs milliseconds. The time bound is interpreted in a coarse-grained, best-effort fashion.
Parameters:
  item - the element to be inserted. Should be non-null.
Parameters:
  msecs - the number of milliseconds to wait. If less thanor equal to zero, the method does not perform any timed waits,but might still requireaccess to a synchronization lock, which can impose unboundeddelay if there is a lot of contention for the channel. true if accepted, else false
exception:
  InterruptedException - if the current thread hasbeen interrupted at a point at which interruptionis detected, in which case the element is guaranteed notto be inserted (i.e., is equivalent to a false return).



peek
public Object peek()(Code)
Return, but do not remove object at head of Channel, or null if it is empty.



poll
public Object poll(long msecs) throws InterruptedException(Code)
Return and remove an item from channel only if one is available within msecs milliseconds. The time bound is interpreted in a coarse grained, best-effort fashion.
Parameters:
  msecs - the number of milliseconds to wait. If less thanor equal to zero, the operation does not perform any timed waits,but might still requireaccess to a synchronization lock, which can impose unboundeddelay if there is a lot of contention for the channel. some item, or null if the channel is empty.
exception:
  InterruptedException - if the current thread hasbeen interrupted at a point at which interruptionis detected, in which case state of the channel is unchanged(i.e., equivalent to a null return).



put
public void put(Object item) throws InterruptedException(Code)
Place item in the channel, possibly waiting indefinitely until it can be accepted. Channels implementing the BoundedChannel subinterface are generally guaranteed to block on puts upon reaching capacity, but other implementations may or may not block.
Parameters:
  item - the element to be inserted. Should be non-null.
exception:
  InterruptedException - if the current thread hasbeen interrupted at a point at which interruptionis detected, in which case the element is guaranteed notto be inserted. Otherwise, on normal return, the element is guaranteedto have been inserted.



take
public Object take() throws InterruptedException(Code)
Return and remove an item from channel, possibly waiting indefinitely until such an item exists. some item from the channel. Different implementationsmay guarantee various properties (such as FIFO) about that item
exception:
  InterruptedException - if the current thread hasbeen interrupted at a point at which interruptionis detected, in which case state of the channel is unchanged.



www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.