Multithreading (b) multithreading Security and synchronization

Source: Internet
Author: User
Tags volatile

One, the environment

Idea

Two. What is a thread safety issue and why is it wired security?

Thread-safety issues arise when multiple threads concurrently access shared resources (typically queries are not generated)

Three. For example

If I wanted to say a few Xunhua plus one, it would eventually increase to 1000. But 5 threads are required to add

classCountImplementsrunnable{Private intCount=1;  Public voidrun () { while(count<=1000) {Count=count+1; System.out.println (Thread.CurrentThread (). GetName ()+ ", Count:" +count); }    }} Public classThreadTest { Public Static voidMain (string[] args) {count Count=NewCount (); Thread T1=NewThread (count, "thread One")); Thread T2=NewThread (count, "thread two"); Thread T3=NewThread (count, "thread three"); Thread T4=NewThread (count, "thread four"); Thread T5=NewThread (count, "thread Five");        T1.start ();        T2.start ();        T3.start ();        T4.start ();    T5.start (); }}

Results:

The code shows that the maximum increase to 1000 loops will end then why does the 1001 appear!!

Since multithreading is now increasing, it is possible that when count is increased to 999, two threads enter the while loop, and then add two more times.

So how to solve it!!!

Four. Use locks to resolve 4.1 synchronous code blocks
classCountImplementsrunnable{Private volatile intCount=1; Private StaticObject OJ =NewObject ();  Public voidrun () { while(count<=1000){            synchronized(OJ) {//OJ is the lock object can be any object or this                if(count<=1000) {Count= Count + 1; System.out.println (Thread.CurrentThread (). GetName ()+ ", Count:" +count); }            }        }    }} Public classThreadTest { Public Static voidMain (string[] args) {count Count=NewCount (); Thread T1=NewThread (count, "thread One")); Thread T2=NewThread (count, "thread two"); Thread T3=NewThread (count, "thread three"); Thread T4=NewThread (count, "thread four"); Thread T5=NewThread (count, "thread Five");        T1.start ();        T2.start ();        T3.start ();        T4.start ();    T5.start (); }}
4.2 Using the Synchronization method
classCountImplementsrunnable{Private volatile intCount=1; Private StaticObject OJ =NewObject ();  Public voidrun () { while(count<=1000) {sole (); }            }     Public synchronized voidSole () {//This lock is the lock object that this        if(count<=1000) {Count= Count + 1; System.out.println (Thread.CurrentThread (). GetName ()+ ", Count:" +count); }    }} Public classThreadTest { Public Static voidMain (string[] args) {count Count=NewCount (); Thread T1=NewThread (count, "thread One")); Thread T2=NewThread (count, "thread two"); Thread T3=NewThread (count, "thread three"); Thread T4=NewThread (count, "thread four"); Thread T5=NewThread (count, "thread Five");        T1.start ();        T2.start ();        T3.start ();        T4.start ();    T5.start (); }}

Multithreading (b) multithreading Security and synchronization

Related Article

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.