Concurrency patterns_Java design pattern

Concurrency patterns prescribe the way access to shared resources is coordinated or sequenced. By far the most common concurrency pattern is Single Thread Execution, where it must be ensured that only one thread has access to a section of code at a time. This section of code is called a critical section, and typically it is a section of code that either obtains access to a resource that must be shared, such as opening a port, or is a sequence of operations that should be atomic, such as obtaining a value, performing calculations, and then updating the value.

The Single Thread Execution pattern

The Singleton pattern we discussed earlier contains two good examples of the Single Thread Execution pattern. The problem motivating this pattern first arises because this example uses lazy instantiation -- delaying instantiating until necessary -- thereby creating the possibility that two different threads may call getInstance() at the same time:

public static synchronized Sequence getInstance()
{
  if(instance==null) // Lazy instantiation
  {
    instance = new Sequence();
  }
  return instance;
}

If this method were not protected against simultaneous access with synchronized, each thread might enter the method, test and find that the static instance reference is null, and each might try to create a new instance. The last thread to finish wins, overwriting the first thread's reference. In this particular example, that might not be so bad -- it only creates an orphaned object that garbage collector will eventually clean up -- but had there been a shared resource that enforced single access, such as opening a port or opening a log file for read/write access, the second thread's attempt to create an instance would have failed because the first thread would have already obtained exclusive access to the shared resource.

Another critical section of code in the Singleton example is the getNext() method:

public static synchronized int getNext()
{
  return ++counter;
}

If this is not protected with synchronized, two threads calling it at the same time might obtain the same current value and not the unique values this class is intended to provide. If this were being used to obtain primary keys for a database insert, the second attempt to insert with same primary key would fail.

As we discussed earlier, you should always consider the cost of using a pattern. Using synchronized works by locking the section of code when it is entered by one thread and blocking any other threads until the first thread is finished. If this is code used frequently by many threads, this could cause a serious degradation in performance.

Another danger is that two threads could become deadlocked if one thread is blocked at one critical section waiting for the second, while the second thread is blocked at another critical section, waiting for the first.

summary

Design patterns are a valuable tool for object-oriented design for a number of important reasons:

  • Patterns provide "...a solution to a problem in a context." ( Design Patterns, Gamma, Helm, Johnson, and Vlissides).

  • Patterns capture the expertise of experienced designers in a methodical way and make them available as design tools and learning tool for non-experts.

  • Patterns provide a vocabulary for discussing object-oriented design at a significant level of abstraction.

  • Patterns catalogs serve as a glossary of idioms that help in understanding common, but complex solutions to design problems.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值