Java tread Multithreading (2) Multithreading security issues

Source: Internet
Author: User

Author: Ching

Original address: http://blog.csdn.net/qingdujun/article/details/39348093


This article demonstrates the tread multithreading security issue, as well as a multi-threaded security approach (thread synchronization).

1) A thread unsafe demo

2) thread synchronization


A small demo demo leads to thread safety issues:

Package THREAD.RUNABLE1.QDJ;//1. Defines the class implementation Runnable interface class RUNDEMO1 implements runnable{private int x = 5;//2. Overwrite the Run method in the Runnable interface//to store the thread code in run public void run () {while (true) {if (x > 0) {//Add sleep (), note: Sleep () throws an exception try { Thread.Sleep (ten);  Let the thread Sleep 10ms} catch (Exception e) {e.printstacktrace ();} System.out.println ("Runnable:" +x);--x;}}} public class CRunableDemo1 {public static void main (string[] args) {RunDemo1 r = new RunDemo1 ()//3. Thread objects are established through the thread class and Runn The subclass object of the able interface as the parameter Thread t1 = new Thread (r); Thread t2 = new Thread (r); thread t3 = new Thread (r); thread T4 = new Thread (r),//4. Using start to open thread T1.start (); T2.start (); T3.start (); T4.start ();}}

Run Display results:


The above print out the 0,-1,-2 and other figures, found not? (Threading Problem!!!) )
What is the cause of the problem?

Thread 1 entered, forced to sleep 10ms, the thread 2 entered, and forced to sleep 10ms, thread 3 entered and forced sleep 10ms, thread 4 into the forced sleep 10ms;

Note that the above 4 threads sleep have entered the IF statement, when entering the time x>0 is still established;

Okay, thread 1 wakes up, starts printing print 5,4,3,2, when--x is not executed, thread 2 wakes up, grabs the CPU execution right ............


Second, thread synchronization

Question: Can we take one such measure for the above question? When thread 1 executes the run code snippet, we do not allow other threads to execute until thread 1 executes and the other threads can enter.

Solution: Fortunately, Java has such a function, the code snippet wrapped up, you can achieve the above problem description of the effect. Function name: synchronized, need a parameter, just pass an object is OK.

Package THREAD.RUNABLE1.QDJ;//1. Define class implementation Runnable interface class RUNDEMO1 implements runnable{private int x = 5;object obj = new Objec T ();//2. Overwrite the Run method in the Runnable interface//Store thread code in run public void run () {while (true) {synchronized (obj) {if (x > 0) {//Add sleep (), Note: Sleep () throws an exception to try {Thread.Sleep (ten);  Let the thread Sleep 10ms} catch (Exception e) {e.printstacktrace ();} System.out.println ("Runnable:" +x);--x;}}}} public class CRunableDemo1 {public static void main (string[] args) {RunDemo1 r = new RunDemo1 ()//3. Thread objects are established through the thread class and Runn The subclass object of the able interface as the parameter Thread t1 = new Thread (r); Thread t2 = new Thread (r); thread t3 = new Thread (r); thread T4 = new Thread (r),//4. Using start to open thread T1.start (); T2.start (); T3.start (); T4.start ();}}
The running results show:



References: Java video Bi Xiangdong presenter

Original address: http://blog.csdn.net/qingdujun/article/details/39348093


Java tread Multithreading (2) Multithreading security issues

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.