/* * Copyright 2023 The Error Prone Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.errorprone.annotations; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Documented; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.Target; /** * This annotation indicates that the class/interface it is applied to is thread safe * *
An object is thread safe if no sequences of accesses (like reads and writes to public fields * or calls to public methods) may put the object into an invalid state, or cause it to violate its * contract, regardless of the interleaving of those actions at runtime. * *
This annotation has two related-but-distinct purposes: * *
Also note that the only easy way to guarantee thread-safety of a class is to make it * immutable, and you should do that whenever possible (or at the least, make as much of the class * be immutable). Otherwise, writing a thread-safe class is inherently tricky and error prone, and * keeping it thread-safe is even more so. * *
The remainder of this javadoc describes the heuristics enforced by {@code ThreadSafeChecker} * and the related {@code com.google.errorprone.bugpatterns.threadsafety.GuardedByChecker} and * {@code com.google.errorprone.bugpatterns.threadsafety.ImmutableChecker} on which the former * relies. * *
The {@code ThreadSafeChecker} heuristics enforce that every field meets at least one of these * requirements: * *
A type is deemed inherently deeply thread-safe if it meets two requirements. The first * requirement is that it meets at least one of these four conditions: * *
This first requirement means the type is at least inherently shallowly thread-safe. * *
Fields annotated with {@code javax.annotation.concurrent.GuardedBy} are likely the meat of a * mutable thread-safe class: these are things that need to be mutated, but should be done so in a * safe manner -- i.e., (most likely) in critical sections of code that protect their access by * means of a lock. See more information in that annotation's javadoc. * *
As stated before, the heuristics above are not sufficient to guarantee the thread safety of a * class. Also as stated before, thread-safety is tricky, and requires diligent analysis by skilled * people. That said, we provide here a few examples of common examples of ways to break these * heuristics, so as to help you avoid them: * *
{@code
* private void incrementMyAtomicInteger() {
* myAtomicInteger.set(myAtomicInteger.get() + 1);
* }
*
* }
*