Demonstrates the problem of data inconsistency caused by Stop violence stop thread, but there are some interesting discoveries (2017-07-03 21:25)

Source: Internet
Author: User

As the note says

/** * Created by weiwei22 on 17/7/3. * * This is mainly to demonstrate the problem of data inconsistency caused by stop. Stop will violently end the thread and release the lock, so it is possible to stop and release the lock when half the data is written. * Read threads At this point it is possible to read inconsistent data by acquiring a lock. * But found a few interesting phenomena: * 1, if m<n, then all of the THREAD1 thread instances will be eliminated without a chance to execute, * because the newly created THREAD1 instance T1 at (1), rest N milliseconds, almost simultaneously the main thread execution to (2), rest m milliseconds , if m<n, means that the main thread will wake up first, * and then first to strong, kill T1; * 2, if m>=n, means that T1 have the opportunity to execute, or will not execute. But if m>n, but m<2n, it means that T1 has only one chance to execute. Because T1 after the completion of a round, immediately into the 2 rounds, but the 2nd round * Sleep is not over, the main thread woke up, and then killed T1; * 3, if m>=2n, then T1 have many times the opportunity to execute, depending on the end is a few times the relationship; */public classThreadMain8 {private static User MU = newUser (); public static void Main (string[] args) throwsinterruptedexception {Thread2 t2 = new Thread2 ("Read Thread");        T2.start (); int index = 1; while (true{Thread1 T1 = new Thread1 ("Write thread" +index); index++; T1.start (); (2) at Thread.Sleep (201);//m msT1.stop (); }} private static class Thread1 extends Thread {public  Thread1 (String name) {Super  (name),} @Override public void  run () {while (true) {synchronized  (MU) {mu.id = (int) (System.currenttimemillis ()/1000);//(1) at try  {thread.sleep (100); n milliseconds} catch  (Interruptedexception e) {e.printstacktrace ();} mu.name =  mu.id; SYSTEMUTIL.P (Thread.CurrentThread (). GetName () + "--write Name =" +  mu.name);}}} private static class Thread2 extends  Thread {public  Thread2 (String name) {Super  (name);} @Override Pub LIC void  Run () {while (true ) {synchronized  (MU) {if (Mu.name! =  mu.id) {SYSTEMUTIL.P (thread.cu Rrentthread (). GetName () + "--Read Name =" + Mu.name + ", id =" +  mu.id);}}}} private static class  user{public int id = 0 , public int name = 0 ;}      

Demonstrates the problem of data inconsistency caused by Stop violence stop thread, but there are some interesting discoveries (2017-07-03 21:25)

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.