Implementation of Java Multi-threaded &&jmeter pressure measurement

Source: Internet
Author: User
Tags volatile

I recently looked at JMeter source code, the multithreading part of the understanding of the following records.

Part1 Threading and Multithreading Concepts


Refer to Line Cheng Sianlai The concept of a process (a container of threads), a process is a running activity of a program with a separate function about a data set. It can apply and own system resources, is a dynamic concept, is an active entity. It is not just the code of the program, it also includes the current activity, which is represented by the value of the program counter and the contents of the processing register.

For JMeter, the running JMeter program instance is a process. A large number of threads are included in the process.

A thread is the smallest unit of a program's execution flow and is a collection of a set of commands. In JMeter, a thread can be used to execute a test case. Based on the basic property settings such as Start-stop, the thread runtime executes the test requests contained in the process according to the sample sampler that the testtree resolves.

Different profiles have incomplete descriptions of thread states, which are basically divided into five states: New Start (wait) run suspend and stop.

In jmeter/, high concurrency is required, with a large number of threads executing in parallel, where each thread represents a vu. This involves the concept of multithreading.

For multi-threading, there are two implementations in Java, that is, one by inheriting the thread class, and the other is implementing the Runnable interface. Due to the Java single Inheritance mechanism, the inheritance thread implementation has more limitations, generally using the Runnable interface method. At the same time, the class that implements the Runnable interface can be converted to run through the thread class construction method public Thread (Runnable target).

The implementation of JMeter is also using the Runnable interface method.

The concept of threading Group (Threadgroup) is required to implement the same tasks as a large number of threads, requiring unified management to configure the state of multithreading.

<ps: Implement Runnable interface than inheritance Thread class has the advantage of:

1 ): Suitable for multiple threads of the same program code to process the same resource

2 ): You can avoid Java The limit of single inheritance in

3 ): Increase the robustness of the program, the code can be shared by multiple threads, code and data Independent. >

Part2: Thread Group

There is a threadgroup concept in Java, and Threadgroup is a class whose purpose is to provide information about thread groups. The Threadgroup API is weak, and it does not provide more functionality than thread. It has two main functions: one is to get the list of active threads in the thread group, and the other is to catch the exception handler for the thread settings.

This weak API does not meet the jmeter requirements, so JMeter is a custom threadgroup class. Used to implement management of a set of threads (that is, concurrent threads of a test instance). It is set for the various properties of the thread group, such as the thread's delay start time/start time/End time/number of threads/number of threads running/thread startup interval. When the thread delay is started, the equivalent daemon threads are created, the corresponding user threads are created by the daemon based on the time and started sequentially, and the user thread is created directly and started without delay.

In addition, thread groups provide schedulethread (this would schedule the time for the Jmeterthread.), scheduling thread initiation, and the ability to control thread-related methods: Start pause stop waiting to stop the thread, get the active Number of threads, and so on.

As mentioned above, the way to implement the Runnable interface is appropriate for multiple threads of the same program code to handle the same resource. Multithreaded shared resources can cause thread-safety problems.

PART3: Thread safety

Thread safety problem Generation principle:

The thread works, the JVM has a main memory, and each thread has its own working memory, a thread to operate on a variable, you have to create a copy in their working memory, After the operation is finished, write to main memory. When multiple threads operate the same variable at the same time, unpredictable results can occur.

For example, two threads add an item to an array at the same time, and the current array length is 0. Two threads simultaneously operate on them, adding a copy in their own working memory, respectively. When two threads are added to return, the array length is already 2, but the array length returned by each thread is still 1, which raises the issue.

Ways to ensure thread safety in JMeter:

A) Define the variables inside the thread before the Start method executes;

Eg:

Start time termination time parameters such as the first assignment, the following JMeter source description:

The following variables is set by Standardjmeterengine.

This is do before start () is called, so the values would be published to The thread safely

The variable of a thread is before the Start method executes, so that the scope of the variable is limited to the thread.

b) Use the volatile keyword to identify the variables that are common to threads (PS: When we use the volatile keyword to modify a variable, the thread will read the variable directly and not cache it);

Eg:

Thread-shared variables such as running/onerrorstoptest are used with the volatile keyword.

c) by synchronized synchronous code block or method body (PS: The key to using synchronized is to create a monitor, this monitor can be modified variable can also be other things you think appropriate such as method, Then, by locking this monitor for thread safety, each thread will have to execute the lock from mainmemory load to Workingmemory, Use&assign, store to Mainme The Mory process will release the lock it gets. This enables the so-called thread safety. );

Eg:

For methods/code blocks, such as static synchronized void incrnumberofthreads () New active threading method, etc., multithreading common code, Need to be synchronized with synchronized.


The author of the multi-threaded understanding is still shallow, the text of improper place please correct, thank you very much!

Implementation of Java Multi-threaded &&jmeter pressure measurement

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.