Software Construction Series (5)

本文探讨了多线程环境下使用集合类时可能遇到的问题,如NullPointerException和IndexOutOfBoundsException等异常。分析了多线程并发修改集合时的潜在风险,并介绍了如何利用JDK提供的线程安全集合来避免此类问题。此外,还对比了使用synchronized修饰和Concurrent包中集合类的不同之处。

在编写多线程的时候遇到这样一个问题,在多个线程中往同一个集合中添加元素,最后在统一遍历进行处理。然而在实际运行中可有能会出现报错:NullPointerException || IndexOutOfBoundsException。然而在添加元素和遍历元素的过程中理应是不可能出现这样的问题的。

但是考虑到多线程,其实可能出现这样一种情况:

List<Integer> l = new ArrayList<>();

public void add(int x) {
  assert l.size() == 0;
  l.add(x);
}

add方法是可以在多线程中正常运行,并且不报错的,即在每个线程执行到add方法时,l.size() == 0。

这样就会在集合同一个位置多次添加,面对更复杂的数据结构,是会出现bug的。

既然知道了原因,就要有相应的解决办法:

JDK提供了对于线程安全的集合类,在JDK1.5之前多线程方面的处理比较简单,只有thread和synchronized两种机制。其中线程安全的集合类是:

List l = Collections.synchronizedList(new ArrayList<>());
Set s=Collections.synchronizedSet(new HashSet());
Map m=Collections.synchronizedMap(new HashMap());
Collection c=Collections.synchronizedCollection(new ArrayList());

可以看到是通过Decorator模式修饰原始集合类,其中原理与关键字synchronized的原理相同,即对这个对象加锁,从而使得只有一个线程能同时使用这个集合。

然而,这一方法的一个显然缺点是:大大降低了多线程的执行效率,在执行这个集合相关的操作时,多线程退化为串行执行。

在JDK1.5中有了更好的解决方案,推出了java.util.concurrent工具包以简化并发。开发者们借助于此,将有效的减少竞争条件(race conditions)和死锁线程。concurrent包很好的解决了这些问题,为我们提供了更实用的并发程序模型。

在JDK1.5中,线程安全的集合类:

import java.util.concurrent;

Collection<> c = new ConcurrentCollection<>();

其中实现线程安全的原理是使用的更加先进的像锁剥离技术。比如在ConcurrentHashMap中会把Map划分为几个片段,只对相关的几个片段上锁,同时允许多线程访问其他未上锁的片段,详细自行查阅JDK技术文档。

转载于:https://www.cnblogs.com/KarlZhang/p/9159876.html

rts Command iwconfig athX rts Description Enable RTS during PPDU construction, based on PPDU size. Sets the minimum packet size for which RTS/CTS protection is used. This setting is used to reduce the amount of arbitration that occurs with short packet transmission, improving throughput. The value of minpktsize is set to the minimum packet size for which to use the RTS/CTS handshake. Setting minpktsize to a value of 0 disables RTS/CTS handshake entirely. The use of RTS/CTS in 802.11n is governed by rate tables and other settings, so this command may not have the desired effect when using 802.11n rates. The threshold should be more than 256 B (as defined by iwconfig). Example usage: iwconfig rts 256 g_enablertscts iwpriv athX g_enablertscts Status of use of RTS/CTS when first transmission attempt fails. rc_retries iwpriv athX rc_retries get_rc_retries iwpriv athX get_rc_retries bcast_rate iwpriv athX bcast_rate get_bcast_rate iwpriv athX get_bcast_rate vht_txmcsmap iwpriv athX vht_txmcsmap g_vht_txmcsmap Configures the number of retries for rate control. For a direct attach data path, software must select a rate series, which consists of four rates and the number of attempts (retries) at each rate. The rate series is used by the WLAN MAC hardware. The MAC hardware first attempts to transmit at the rate of the first entry in the series. If it fails, the transmission is repeated at that rate for a total number of attempts equal to the number of retries specified in the series. If all attempts at that rate fail, the MAC hardware drops down to the next rate in the series and repeats. Displays the number of retries for rate control. Specifies the broadcast rate of transmission. Displays the configured broadcast rate of transmission. Specifies the Tx MCS Map field of VHT Capabilities IE. iwpriv athX g_vht_txmcsmap Displays the Tx MCS Map field of VHT Capabilities IE. vht_rxmcsmap iwpriv athX vht_rxmcsmap Specifies the Rx MCS Map field of VHT Capabilities IE. g_vht_rxmcsmap iwpriv athX g_vht_rxmcsmap Displays the Rx MCS Map field of VHT Capabilities IE. get_parent iwpriv athX get_parent set_onetxchain iwpriv athX set_onetxchain set_cabq_maxdur Retrieves the parent AP, which is the device to which the client is actually connected. Parent Wi-Fi of an AP is the wifi which the user originally configures as extender of the AP. Specifies that only one Tx chain be used for transmission. iwpriv athX set_cabq_maxdur Specifies the maximum period of time for the content after beacon queue (CABQ). get_acs_state iwpriv athX get_acs_state get_cac_state iwpriv athX get_cac_state implicitbf iwpriv athX implicitbf get_implicitbf iwpriv athX get_implicitbf Retrieves the state of the automatic channel selection (ACS) reporting functionality. Retrieves the status of the channel availability check (CAC) mechanism. Enable (1) or disable (0) implicit beamforming. Retrieves the configuration of implicit beamforming.
09-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值