KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > util > ThreadLocalCounter


1 /*
2  * ThreadLocalCounter.java
3  *
4  * Created on 25. Juli 2006, 13:39
5  */

6 /*
7  * Copyright 2006 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.util;
23
24 /**
25  * A counter which's value is local to each thread.
26  * Its initial value is <code>0</code>.
27  *
28  * @author Christian Schlichtherle
29  * @version @version@
30  * @since TrueZIP 6.2
31  */

32 public class ThreadLocalCounter extends ThreadLocal JavaDoc {
33
34     public final int getCounter() {
35         return ((Holder) get()).count;
36     }
37
38     public final void setCounter(int count) {
39         ((Holder) get()).count = count;
40     }
41
42     public final void increment() {
43         ((Holder) get()).count++;
44     }
45
46     public final void decrement() {
47         ((Holder) get()).count--;
48     }
49
50     protected final Object JavaDoc initialValue() {
51         return new Holder();
52     }
53
54     private static final class Holder {
55         int count;
56     }
57 }
58
Popular Tags