KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > nio > channels > spi > AbstractSelectionKey


1 /*
2  * @(#)AbstractSelectionKey.java 1.12 06/07/15
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.nio.channels.spi;
9
10 import java.nio.channels.*;
11
12
13 /**
14  * Base implementation class for selection keys.
15  *
16  * <p> This class tracks the validity of the key and implements cancellation.
17  *
18  * @author Mark Reinhold
19  * @author JSR-51 Expert Group
20  * @version 1.12, 06/07/15
21  * @since 1.4
22  */

23
24 public abstract class AbstractSelectionKey
25     extends SelectionKey
26 {
27
28     /**
29      * Initializes a new instance of this class. </p>
30      */

31     protected AbstractSelectionKey() { }
32
33     private volatile boolean valid = true;
34
35     public final boolean isValid() {
36     return valid;
37     }
38
39     void invalidate() { // package-private
40
valid = false;
41     }
42
43     /**
44      * Cancels this key.
45      *
46      * <p> If this key has not yet been cancelled then it is added to its
47      * selector's cancelled-key set while synchronized on that set. </p>
48      */

49     public final void cancel() {
50         // Synchronizing "this" to prevent this key from getting canceled
51
// multiple times by different threads, which might cause race
52
// condition between selector's select() and channel's close().
53
synchronized (this) {
54         if (valid) {
55                 valid = false;
56                 ((AbstractSelector JavaDoc)selector()).cancel(this);
57         }
58     }
59     }
60 }
61
Popular Tags