@@ -2226,7 +2226,238 @@ public class DiningPhilosophers {
22262226< ! -- Constructors are not Thread - Safe -- >
22272227## 构造函数非线程安全
22282228
2229+ 当你在脑子里想象一个对象构造的过程,你会很容易认为这个过程是线程安全的。毕竟,在对象初始化完成前没人能见到这个对象,所以又怎么会产生对于这个对象的争议呢?确实,java语言规范(JLS )自信满满地陈述道:“没有必要使构造器同步,因为它会锁定正在构造的对象,而这通常会使得该对象直到其所有构造器完成所有工作后,才对其他线程可见。”
2230+ 不幸的是,对象构造过程像其他任何事物一样容易受到共享内存并发问题的影响,只是作用机制可能更微妙罢了。
2231+ 考虑使用静态字段为每个对象自动创建唯一标识符的过程。为了测试其不同的实现过程,我们从一个接口开始:
22292232
2233+ ```java
2234+ // concurrent/HasID.java
2235+ public interface HasID {
2236+ int getID ();
2237+ }
2238+ ```
2239+
2240+ 然后StaticIDField 类以显式方式实现该接口:
2241+
2242+ ```java
2243+ // concurrent/StaticIDField.java
2244+ public class StaticIDField implements HasID {
2245+ private static int counter = 0 ;
2246+ private int id = counter++ ;
2247+ public int getID () { return id; }
2248+ }
2249+ ```
2250+
2251+ 正如您所想的,此类是一个简单无害的类,它甚至没有一个显式的构造器来引发问题。当我们运行多个用于创建此类对象的线程时,究竟会发生什么,为了搞清楚这点,我们做了以下测试:
2252+
2253+ ```java
2254+ // concurrent/IDChecker.java
2255+ import java. util. * ;
2256+ import java. util. function. * ;
2257+ import java. util. stream. * ;
2258+ import java. util. concurrent. * ;
2259+ import com.google.common.collect. Sets ;
2260+ public class IDChecker {
2261+ public static final int SIZE = 100_000 ;
2262+
2263+ static class MakeObjects implements
2264+ Supplier<List<Integer > > {
2265+ private Supplier<HasID > gen;
2266+
2267+ MakeObjects (Supplier<HasID > gen ) {
2268+ this . gen = gen;
2269+ }
2270+
2271+ @Override public List<Integer > get () {
2272+ return Stream . generate(gen)
2273+ .limit(SIZE )
2274+ .map(HasID :: getID)
2275+ .collect(Collectors . toList());
2276+ }
2277+ }
2278+
2279+ public static void test (Supplier<HasID > gen ) {
2280+ CompletableFuture<List<Integer > >
2281+ groupA = CompletableFuture . supplyAsync(new
2282+ MakeObjects (gen)),
2283+ groupB = CompletableFuture . supplyAsync(new
2284+ MakeObjects (gen));
2285+
2286+ groupA. thenAcceptBoth(groupB, (a, b) - > {
2287+ System . out. println(
2288+ Sets . intersection(
2289+ Sets . newHashSet(a),
2290+ Sets . newHashSet(b)). size());
2291+ }). join();
2292+ }
2293+ }
2294+ ```
2295+
2296+ MakeObjects 类是一个供应者类,包含一个能够产生List \< Integer > 类型的列表对象的get()方法。通过从每个HasID 对象提取ID 并放入列表中来生成这个列表对象,而test()方法则创建了两个并行的CompletableFuture 对象,用于运行MakeObjects 供应者类,然后获取运行结果。
2297+ 使用Guava 库中的Sets . intersection()方法,计算出这两个返回的List \< Integer > 对象中有多少相同的ID (使用谷歌Guava 库里的方法比使用官方的retainAll()方法速度快得多)。
2298+
2299+ 现在我们可以测试上面的StaticIDField 类了:
2300+
2301+ ```java
2302+ // concurrent/TestStaticIDField.java
2303+ public class TestStaticIDField {
2304+
2305+ public static void main (String [] args ) {
2306+ IDChecker . test(StaticIDField :: new );
2307+ }
2308+ }
2309+ /* Output:
2310+ 13287
2311+ */
2312+ ```
2313+
2314+ 结果中出现了很多重复项。很显然,纯静态int 用于构造过程并不是线程安全的。让我们使用AtomicInteger 来使其变为线程安全的:
2315+
2316+ ```java
2317+ // concurrent/GuardedIDField.java
2318+ import java. util. concurrent. atomic. * ;
2319+ public class GuardedIDField implements HasID {
2320+ private static AtomicInteger counter = new
2321+ AtomicInteger ();
2322+
2323+ private int id = counter. getAndIncrement();
2324+
2325+ public int getID () { return id; }
2326+
2327+ public static void main (String [] args ) { IDChecker . test(GuardedIDField :: new );
2328+ }
2329+ }
2330+ /* Output:
2331+ 0
2332+ */
2333+ ```
2334+
2335+ 构造器有一种更微妙的状态共享方式:通过构造器参数:
2336+
2337+ ```java
2338+ // concurrent/SharedConstructorArgument.java
2339+ import java. util. concurrent. atomic. * ;
2340+ interface SharedArg {
2341+ int get ();
2342+ }
2343+
2344+ class Unsafe implements SharedArg {
2345+ private int i = 0 ;
2346+
2347+ public int get (){
2348+ return i++ ;
2349+ }
2350+ }
2351+
2352+ class Safe implements SharedArg {
2353+ private static AtomicInteger counter = new AtomicInteger ();
2354+
2355+ public int get (){
2356+ return counter. getAndIncrement();
2357+ }
2358+ }
2359+
2360+ class SharedUser implements HasID {
2361+ private final int id;
2362+
2363+ SharedUser (SharedArg sa ){
2364+ id = sa. get();
2365+ }
2366+
2367+ @Override
2368+ public int getID (){
2369+ return id;
2370+ }
2371+ }
2372+
2373+ public class SharedConstructorArgument {
2374+ public static void main (String [] args ){
2375+ Unsafe unsafe = new Unsafe ();
2376+ IDChecker . test(() - > new SharedUser (unsafe));
2377+
2378+ Safe safe = new Safe ();
2379+ IDChecker . test(() - > new SharedUser (safe));
2380+ }
2381+ }
2382+ /* Output:
2383+ 24838
2384+ 0
2385+ */
2386+ ```
2387+
2388+ 在这里,SharedUser 构造器实际上共享了相同的参数。即使SharedUser 以完全无害且合理的方式使用其自己的参数,其构造器的调用方式也会引起冲突。SharedUser 甚至不知道它是以这种方式调用的,更不必说控制它了。
2389+ 同步构造器并不被java语言所支持,但是通过使用同步语块来创建你自己的同步构造器是可能的(请参阅附录:Low - Level Concurrency ,来进一步了解同步关键字——synchronized )。尽管JLS (java语言规范)这样陈述道:“……它会锁定正在构造的对象”,但这并不是真的——构造器实际上只是一个静态方法,因此同步构造器实际上会锁定该类的Class 对象。我们可以通过创建自己的静态对象并锁定它,来达到与同步构造器相同的效果:
2390+
2391+ ```java
2392+ // concurrent/SynchronizedConstructor.java
2393+
2394+ import java. util. concurrent. atomic. * ;
2395+
2396+ class SyncConstructor implements HasID {
2397+ private final int id;
2398+ private static Object constructorLock =
2399+ new Object ();
2400+
2401+ SyncConstructor (SharedArg sa ){
2402+ synchronized (constructorLock){
2403+ id = sa. get();
2404+ }
2405+ }
2406+
2407+ @Override
2408+ public int getID (){
2409+ return id;
2410+ }
2411+ }
2412+
2413+ public class SynchronizedConstructor {
2414+ public static void main (String [] args ){
2415+ Unsafe unsafe = new Unsafe ();
2416+ IDChecker . test(() - > new SyncConstructor (unsafe));
2417+ }
2418+ }
2419+ /* Output:
2420+ 0
2421+ */
2422+
2423+ ```
2424+
2425+ Unsafe 类的共享使用现在就变得安全了。另一种方法是将构造器设为私有(因此可以防止继承),并提供一个静态Factory 方法来生成新对象:
2426+
2427+ ```java
2428+ // concurrent/SynchronizedFactory.java
2429+ import java. util. concurrent. atomic. * ;
2430+
2431+ final class SyncFactory implements HasID {
2432+ private final int id;
2433+
2434+ private SyncFactory (SharedArg sa ){
2435+ id = sa. get();
2436+ }
2437+
2438+ @Override
2439+ public int getID (){
2440+ return id;
2441+ }
2442+
2443+ public static synchronized SyncFactory factory (SharedArg sa ){
2444+ return new SyncFactory (sa);
2445+ }
2446+ }
2447+
2448+ public class SynchronizedFactory {
2449+ public static void main (String [] args ){
2450+ Unsafe unsafe = new Unsafe ();
2451+ IDChecker . test(() - > SyncFactory . factory(unsafe));
2452+ }
2453+ }
2454+ /* Output:
2455+ 0
2456+ */
2457+ ```
2458+
2459+ 通过同步静态工厂方法,可以在构造过程中锁定Class 对象。
2460+ 这些示例充分表明了在并发Java 程序中检测和管理共享状态有多困难。即使您采取“不共享任何内容”的策略,也很容易产生意外的共享事件。
22302461< ! -- Effort , Complexity ,Cost -- >
22312462## 复杂性和代价
22322463
0 commit comments