parallelism ,parall...

ON PARALLELISM--《JOURNAL OF NORTHWEST INSTITUTE OF LIGHT INDUSTRY》1996年02期
ON PARALLELISM
Zhai X Li XYang Li
This paper,by listing rhetorical functions of parallel structure,points out that the right use of parallelism in an article plays an important part in achieving coherence,richness and vividness,and expressing deeply and convincingly.Thus,on the one hand,it may lend character and uniqueness to the article.On the other hand,the reader may touch the beauty of the writing.
【Key Words】:
【CateGory Index】:
supports all the CNKI
only supports the PDF format.
【Co-citations】
Chinese Journal Full-text Database
Li P[J];Journal of Anhui Vocational College of Electronics & Information T2008-03
WANG Hui-ping (Foreign Language Department, Anqing Teachers College,Anqing 246011,China);[J];Journal of Anqing Teachers College(Social Science Edition);2001-02
ZHANG Xiao-hui
(Dept. of Foreign Language, Anshan University of Science and Technology,Anshan 114044,China);[J];Journal of Anshan University of Science and T2003-02
LI Guan-xue(Faculty of Foreign Languages,Henan University,Kaifeng 475001,China);[J];Journal of Aayang Teachers C2002-03
ZHAI Yan-xia(Agricultural University of Henan,Zhengzhou 450002,China);[J];Journal of Aayang Teachers C2002-03
ZHAO Yong-mei,HAN Yu-huan(Department of Foreign Language,Baicheng Normal College,Baicheng 137000,China);[J];Journal of Baicheng Normal C2007-01
Wang Yuhuan (English Department of Bijie Teachers College,Bijie,Guizhou,551700);[J];Journal of Bijie Teachers C2003-01
Liu Wei, Chen Zhongcai First-author's address Editorial Department of Chemical Journal of Chinese Universities, 130023, Changchun, C[J];Acta E2003-04
(Binzhou Teachers College,Binzhou 256604,China);[J];Journal of Binzhou Teachers C2002-01
ZHENG Yin-fang(College English Teaching Department,Hunan University of Science and Engineering,Yongzhong,425100,China);[J];Journal of Hunan University of Arts and Science(Social Science Edition);2008-04
【Co-references】
Chinese Journal Full-text Database
LI Yun F[J];JOURNAL OF ANHUI TV UNIVERSITY;2000-01
(Shaoyang Teachers′ Advanced Studies School, Shaoyang 410075,China);[J];JOURNAL OF CHANGSHA UNIVERSITY OF ELECTRIC POWER;2000-02
WANG Qi,LUO Shang-rong(School of Humanities and Social Sciences,East China Jiaotong Uni.,Nanchang 330013,China);[J];Journal of East China Jiaotong U2004-06
HE Ling?feng? (Department of Chinese Language and Literature, Yichun University, Yichun 336000, P.R.China)??;[J];Journal of Shandong University(Philosophy and Social Sciences);2003-03
LI Min,LIU Gui-zhi(1.Department of Law and Public Management,Shijiazhuang Vocational Technology Institute,Shijiazhuang .English Office,Hengshui Vocational Education Centre,Hengshui 053000,China);[J];Journal of Shijiazhuang Vocational Technology I2006-03
Wu Dingjun ( Dewang Middle School, Jiangkou Guizhou 554400 );[J];Journal of Tongren Teachers C2004-S1
Sheng-qiao(Chinese Department, Xianyang Teachers' College, Xianyang
Shaanxi 712000,
);[J];Journal of Xianyang Teachers C2004-01
Similar Journals
(C)2006 Tsinghua Tongfang Knowledge Network Technology Co., Ltd.(Beijing)(TTKN) All rights reservedDocumentation
The Java& Tutorials
Parallelism
Parallel computing involves dividing a problem into subproblems, solving those problems simultaneously (in parallel, with each subproblem running in a separate thread), and then combining the results of the solutions to the subproblems. Java SE provides the
, which enables you to more easily implement parallel computing in your applications. However, with this framework, you must specify how the problems are subdivided (partitioned). With aggregate operations, the Java runtime performs this partitioning and combining of solutions for you.
One difficulty in implementing parallelism in applications that use collections is that collections are not thread-safe, which means that multiple threads cannot manipulate a collection without introducing
. The Collections Framework provides
, which add automatic synchronization to an arbitrary collection, making it thread-safe. However, synchronization introduces
. You want to avoid thread contention because it prevents threads from running in parallel. Aggregate operations and parallel streams enable you to implement parallelism with non-thread-safe collections provided that you do not modify the collection while you are operating on it.
Note that parallelism is not automatically faster than performing operations serially, although it can be if you have enough data and processor cores. While aggregate operations enable you to more easily implement parallelism, it is still your responsibility to determine if your application is suitable for parallelism.
This section covers the following topics:
You can find the code excerpts described in this section in the example
You can execute streams in serial or in parallel. When a stream executes in parallel, the Java runtime partitions the stream into multiple substreams. Aggregate operations iterate over and process these substreams in parallel and then combine the results.
When you create a stream, it is always a serial stream unless otherwise specified. To create a parallel stream, invoke the operation
. Alternatively, invoke the operation
. For example, the following statement calculates the average age of all male members in parallel:
double average = roster
.parallelStream()
.filter(p -> p.getGender() == Person.Sex.MALE)
.mapToInt(Person::getAge)
.average()
.getAsDouble();
Consider again the following example (which is described in the section
) that groups members by gender. This example invokes the collect operation, which reduces the collection roster into a Map:
Map&Person.Sex, List&Person&& byGender =
Collectors.groupingBy(Person::getGender));
The following is the parallel equivalent:
ConcurrentMap&Person.Sex, List&Person&& byGender =
.parallelStream()
Collectors.groupingByConcurrent(Person::getGender));
This is called a concurrent reduction. The Java runtime performs a concurrent reduction if all of the the following are true for a particular pipeline that contains the collect operation:
The stream is parallel.
The parameter of the collect operation, the collector, has the characteristic
. To determine the characteristics of a collector, invoke the
Either the stream is unordered, or the collector has the characteristic
. To ensure that the stream is unordered, invoke the
operation.
Note: This example returns an instance of
instead of Map and invokes the
operation instead of groupingBy. (See the section
for more information about ConcurrentMap.) Unlike the operation groupingByConcurrent, the operation groupingBy performs poorly with parallel streams. (This is because it operates by merging two maps by key, which is computationally expensive.) Similarly, the operation
performs better with parallel streams than the operation
The order in which a pipeline processes the elements of a stream depends on whether the stream is executed in serial or in parallel, the source of the stream, and intermediate operations. For example, consider the following example that prints the elements of an instance of ArrayList with the forEach operation several times:
Integer[] intArray = {1, 2, 3, 4, 5, 6, 7, 8 };
List&Integer& listOfIntegers =
new ArrayList&&(Arrays.asList(intArray));
System.out.println("listOfIntegers:");
listOfIntegers
.forEach(e -& System.out.print(e + " "));
System.out.println("");
System.out.println("listOfIntegers sorted in reverse order:");
Comparator&Integer& normal = Integer::
Comparator&Integer& reversed = normal.reversed();
Collections.sort(listOfIntegers, reversed);
listOfIntegers
.forEach(e -& System.out.print(e + " "));
System.out.println("");
System.out.println("Parallel stream");
listOfIntegers
.parallelStream()
.forEach(e -& System.out.print(e + " "));
System.out.println("");
System.out.println("Another parallel stream:");
listOfIntegers
.parallelStream()
.forEach(e -& System.out.print(e + " "));
System.out.println("");
System.out.println("With forEachOrdered:");
listOfIntegers
.parallelStream()
.forEachOrdered(e -> System.out.print(e + " "));
System.out.println("");
This example consists of five pipelines. It prints output similar to the following:
listOfIntegers:
1 2 3 4 5 6 7 8
listOfIntegers sorted in reverse order:
8 7 6 5 4 3 2 1
Parallel stream:
3 4 1 6 2 5 7 8
Another parallel stream:
6 3 1 5 7 8 4 2
With forEachOrdered:
8 7 6 5 4 3 2 1
This example does the following:
The first pipeline prints the elements of the list listOfIntegers in the order that they were added to the list.
The second pipeline prints the elements of listOfIntegers after it was sorted by the method
The third and fourth pipelines print the elements of the list in an apparently random order. Remember that stream operations use internal iteration when processing elements of a stream. Consequently, when you execute a stream in parallel, the Java compiler and runtime determine the order in which to process the stream's elements to maximize the benefits of parallel computing unless otherwise specified by the stream operation.
The fifth pipeline uses the method
, which processes the elements of the stream in the order specified by its source, regardless of whether you executed the stream in serial or parallel. Note that you may lose the benefits of parallelism if you use operations like forEachOrdered with parallel streams.
A method or an expression has a side effect if, in addition to returning or producing a value, it also modifies the state of the computer. Examples include
mutable reductions (operations that use the collect see the section
for more information) as well as invoking the System.out.println method for debugging. The JDK handles certain side effects in pipelines well. In particular, the collect method is designed to perform the most common stream operations that have side effects in a parallel-safe manner. Operations like forEach and peek are desig a lambda expression that returns void, such as one that invokes System.out.println, can do nothing but have side effects. Even so, you should use the forEach and peek
if you use one of these operations with a parallel stream, then the Java runtime may invoke the lambda expression that you specified as its parameter concurrently from multiple threads. In addition, never pass as parameters lambda expressions that have side effects in operations such as filter and map. The following sections discuss
and , both of which can be sources of side effects and can return inconsistent or unpredictable results, especially in parallel streams. However, the concept of
is discussed first, because it has a direct effect on interference.
All intermediate operations are lazy. An expression, method, or algorithm is lazy if its value is evaluated only when it is required. (An algorithm is eager if it is evaluated or processed immediately.) Intermediate operations are lazy because they do not start processing the contents of the stream until the terminal operation commences. Processing streams lazily enables the Java compiler and runtime to optimize how they process streams. For example, in a pipeline such as the filter-mapToInt-average example described in the section
, the average operation could obtain the first several integers from the stream created by the mapToInt operation, which obtains elements from the filter operation. The average operation would repeat this process until it had obtained all required elements from the stream, and then it would calculate the average.
Lambda expressions in stream operations should not interfere. Interference occurs when the source of a stream is modified while a pipeline processes the stream.
For example, the following code attempts to concatenate the strings contained in the List listOfStrings. However, it throws a ConcurrentModificationException:
List&String& listOfStrings =
new ArrayList&&(Arrays.asList("one", "two"));
// This will fail as the peek operation will attempt to add the
// string "three" to the source after the terminal operation has
// commenced.
String concatenatedString = listOfStrings
// Don't do this! Interference occurs here.
.peek(s -& listOfStrings.add("three"))
.reduce((a, b) -& a + " " + b)
System.out.println("Concatenated string: " + concatenatedString);
} catch (Exception e) {
System.out.println("Exception caught: " + e.toString());
This example concatenates the strings contained in listOfStrings into an Optional&String& value with the reduce operation, which is a terminal operation. However, the pipeline here invokes the intermediate operation peek, which attempts to add a new element to listOfStrings. Remember, all intermediate operations are lazy. This means that the pipeline in this example begins execution when the operation get is invoked, and ends execution when the get operation completes. The argument of the peek operation attempts to modify the stream source during the execution of the pipeline, which causes the Java runtime to throw a ConcurrentModificationException.
Avoid using stateful lambda expressions as parameters in stream operations. A stateful lambda expression is one whose result depends on any state that might change during the execution of a pipeline. The following example adds elements from the List listOfIntegers to a new List instance with the map intermediate operation. It does this twice, first with a serial stream and then with a parallel stream:
List&Integer& serialStorage = new ArrayList&&();
System.out.println("Serial stream:");
listOfIntegers
// Don't do this! It uses a stateful lambda expression.
.map(e -& { serialStorage.add(e); })
.forEachOrdered(e -& System.out.print(e + " "));
System.out.println("");
serialStorage
.forEachOrdered(e -& System.out.print(e + " "));
System.out.println("");
System.out.println("Parallel stream:");
List&Integer& parallelStorage = Collections.synchronizedList(
new ArrayList&&());
listOfIntegers
.parallelStream()
// Don't do this! It uses a stateful lambda expression.
.map(e -& { parallelStorage.add(e); })
.forEachOrdered(e -& System.out.print(e + " "));
System.out.println("");
parallelStorage
.forEachOrdered(e -& System.out.print(e + " "));
System.out.println("");
The lambda expression e -& { parallelStorage.add(e); } is a stateful lambda expression. Its result can vary every time the code is run. This example prints the following:
Serial stream:
8 7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
Parallel stream:
8 7 6 5 4 3 2 1
1 3 6 2 4 5 8 7
The operation forEachOrdered processes elements in the order specified by the stream, regardless of whether the stream is executed in serial or parallel. However, when a stream is executed in parallel, the map operation processes elements of the stream specified by the Java runtime and compiler. Consequently, the order in which the lambda expression e -& { parallelStorage.add(e); } adds elements to the List parallelStorage can vary every time the code is run. For deterministic and predictable results, ensure that lambda expression parameters in stream operations are not stateful.
Note: This example invokes the method
so that the List parallelStorage is thread-safe. Remember that collections are not thread-safe. This means that multiple threads should not access a particular collection at the same time. Suppose that you do not invoke the method synchronizedList when creating parallelStorage:
List&Integer& parallelStorage = new ArrayList&&();
The example behaves erratically because multiple threads access and modify parallelStorage without a mechanism like synchronization to schedule when a particular thread may access the List instance. Consequently, the example could print output similar to the following:
Parallel stream:
8 7 6 5 4 3 2 1
null 3 5 4 7 8 1 2京 东 价:
PLUS会员专享价
支  持:
重  量:
搭配赠品:
所 在 地:北京 海淀区
服务支持:
加载中,请稍候...
加载中,请稍候...
加载中,请稍候...
【预订】Studies in Biblical Parallelism. PT. I.
商品介绍加载中...
扫一扫,精彩好书免费看
服务承诺:
京东平台卖家销售并发货的商品,由平台卖家提供发票和相应的售后服务。请您放心购买!
注:因厂家会在没有任何提前通知的情况下更改产品包装、产地或者一些附件,本司不能确保客户收到的货物与商城图片、产地、附件说明完全一致。只能确保为原厂正货!并且保证与当时市场上同样主流新品一致。若本商城没有及时更新,请大家谅解!
权利声明:京东上的所有商品信息、客户评价、商品咨询、网友讨论等内容,是京东重要的经营资源,未经许可,禁止非法转载使用。
注:本站商品信息均来自于合作方,其真实性、准确性和合法性由信息拥有者(合作方)负责。本站不提供任何保证,并不承担任何法律责任。
印刷版次不同,印刷时间和版次以实物为准。
价格说明:
京东价:京东价为商品的销售价,是您最终决定是否购买商品的依据。
划线价:商品展示的划横线价格为参考价,该价格可能是品牌专柜标价、商品吊牌价或由品牌供应商提供的正品零售价(如厂商指导价、建议零售价等)或该商品在京东平台上曾经展示过的销售价;由于地区、时间的差异性和市场行情波动,品牌专柜标价、商品吊牌价等可能会与您购物时展示的不一致,该价格仅供您参考。
折扣:如无特殊说明,折扣指销售商在原价、或划线价(如品牌专柜标价、商品吊牌价、厂商指导价、厂商建议零售价)等某一价格基础上计算出的优惠比例或优惠金额;如有疑问,您可在购买前联系销售商进行咨询。
异常问题:商品促销信息以商品详情页“促销”栏中的信息为准;商品的具体售价以订单结算页价格为准;如您发现活动商品售价或促销信息有异常,建议购买前先联系销售商咨询。
加载中,请稍候...
加载中,请稍候...
加载中,请稍候...
加载中,请稍候...
加载中,请稍候...
加载中,请稍候...
加载中,请稍候...
浏览了该商品的用户还浏览了
加载中,请稍候...
iframe(src='///ns.html?id=GTM-T947SH', height='0', width='0', style='display: visibility:')}

我要回帖

更多关于 faulty parallelism 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信