Test of Java Concurrent Programming (iii)

Source: Internet
Author: User

Generate more alternating operations

Because errors that occur in concurrent code are generally low probability events, many times are required to test for concurrency errors, but there are ways to increase the probability of discovering these errors, as mentioned earlier, on multiprocessor systems, if the number of processors is less than the number of active threads, then a single-processor system or Systems that contain more than one processor can produce more alternating behavior.

There is a useful way to increase the number of alternating operations. In order to search the state space of the program more efficiently: It is to add Thread.yield as an empty operation in the operation of access state. When the code does not use enough synchronization to access the state, there will be some errors that are sensitive to the timing of the execution, which can be exposed by invoking the yield method during the execution of an operation. This approach requires adding some calls to the test and deleting them in the official product LV.

Public synchronized void Tranfercredits (account from,account to,int amount) {from.setbalance (from.getbalance ()-amount ); if (Random.nextint (>threshold) {Thread.yield ();} To.setbalance (To.getbalance () +amount);}



Performance Testing

Performance testing should conform to the current application scenario, ideally reflecting the actual usage of the object being tested in the application.

The second goal is to adjust the various limits based on experience values, such as the number of threads, cache capacity, and so on, which are dependent on the platform features, and we usually choose these values reasonably so that the program can run well on more systems.


Comparison of multiple algorithms

The test results show that the scalability of Linkedblockgingqueue is higher than that of Arrayblockingqueue, it seems strange at first, the list queue must be assigned a list node object each time the element is inserted, which seems to be more than the array-based queue. The put and take methods of a linked list queue support more concurrency, because some of the optimized link queue algorithms can share the update operation of the queue head node with the update operation of the tail node. Therefore, if the algorithm can reduce the level of competition by performing more memory allocation operations, the algorithm usually has higher scalability.



Test of Java Concurrent Programming (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.