Understanding Threads (thread safety)

Source: Internet
Author: User

Thread safety, which means that the current operation is atomic.

Atomic Operation Thread Safety

stringbuffer is thread safe, and StringBuilder is thread insecure

if the operation for StringBuffer is thread-safe, however, the thread security that Java gives you is to say that its method is to execute is exclusive

Rather than multiple invocations of the object itself, it is still safe to

Example

Take a look at the following example:
has a data member in Stringbuffertest contents it is for expansion, each append is thread-safe, but the combination of many append is not thread-safe

If you switch to Stringbuider or even append to half, it will also allow the other threads to operate on this basis:

public class Stringbuffertest {Private StringBuffer contents =New StringBuffer ();PublicvoidLogString message) {contents.Append (System.currenttimemillis ()); Contents.Append"; "); Contents.Append (Thread.CurrentThread (). GetName ());Forint i=0;i<10000;i++) {contents.Append (i); Contents.Append (message);The append itself is thread-safe and cannot be accessed by other threads when modifying contents. Contents.Append"\ n"); } contents.Append"\ n"); }Publicvoid getcontents () {System.out.println (contents); }}class Runthread extends thread{String message; Stringbuffertest buffer;Public Runthread (stringbuffertest buffer, String message) { this.buffer = buffer; this.message = message;} public Void Run () {while (true) {buffer.  log (message); Buffer.getcontents (); }} public static void Main (string[] args) {stringbuffertest ss = new Stringbuffertest (); New Runthread (ss, "You"). Start (); New Runthread (ss, "Me"). Start (); New Runthread (SS, "she"). Start ();}}             
Multithreading security

stringbuilder and StringBuffer methods are exactly the same, which is a multithreaded and single threaded problem

Multiple threads call the Append method of the same stringbuffer, which is not related to whether he is thread safe

unless your result is append a string of strings is messed up, that means he's not thread-safe

thread safety means that only one thread accesses a critical resource at any moment

thread safety is not to say that his sequence of operations is synchronous

Just don't allow other threads to change

when he executes a method.

for a class it is not thread-safe to see that multiple threads are running concurrently, and these threads may execute a method at the same time

But the result of each run is the same as the results of single-threaded execution, so it can be said that thread-safe

Since the log method is not locked, each now can get the execution fragment of the CPU after the append lock is released.

Avoid misunderstanding multi-line security

But don't misunderstand multithreading security:

public String toString () {
StringBuffer buffer = new StringBuffer ();
Buffer.append (' < ');
Buffer.append (this.name);
Buffer.append (' > ');
return buffer.tostring ();
}

This code is fully thread-safe

stringbuffer itself is thread-safe!

thread safety that is the functionality provided by the class itself is secure

you provide inserting a string, then this string insertion is safe

But to insert two strings, two order you to set, between this if there is another insert error, regardless of the class thing, is your own code problem

Turn from

Those classes in the Java collection are thread-safe, MEXICAN_OK

Understanding Threads (thread safety)

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.