7. How to share data among multiple threads-jdk5 multithreading

Source: Internet
Author: User

1 /**2 * Discussion on how to share data among multiple threads3  * @authorlitaiqing4  * 5  * *************************************************6 * If each thread executes a different code, this time requires a different runnable object, with7 * There are two ways to achieve data sharing between these runnble objects:8 * 1. Encapsulate the shared data in another object and pass the object to the9 * Individual Runnble objects. Each thread's method of manipulating shared data is also assigned to that object.Ten * go up and finish, so it's easy to achieve mutual exclusion and communication for each operation on the data.  One * 2. Use these runnable objects as internal classes in a class and share the data as A * This is a member variable in the external class, and each thread also assigns an action method to the shared data to the external class . - * In order to achieve mutual exclusion and communication of various operations on shared data, as individual runnble of internal classes - * Objects call these methods of the external class.  the * Combination of the above two ways: encapsulate shared data in another object, each thread - * The method of sharing data is also assigned to the object to be completed, the object as the outer class of the - * A local variable in a member variable or method, each thread's Runnable object as an outer class - * member inner class or local inner class.  + * In short, the very short code to synchronize mutually exclusive is best placed in several separate methods, these - * methods are then placed in the same class, which makes it easier to synchronize mutual exclusion and communication between them.  +  * ************************************************* A  * at  */ -  Public classMultithreadsharedata { -      -     Private StaticShareData1 data1 =NewShareData1 (); -      -      Public Static voidMain (string[] args) { in          -         FinalShareData1 data2 =NewShareData1 (); to         NewThread (NewMyRunnable1 (data1)). Start (); +         NewThread (NewMyRunnable1 (data2)). Start (); -          the         FinalShareData1 data1 =NewShareData1 (); *         NewThread (NewRunnable () { $ @OverridePanax Notoginseng              Public voidrun () { - data1.decrement (); the             } + }). Start (); A         NewThread (NewRunnable () { the @Override +              Public voidrun () { - data1.increment (); $             } $ }). Start (); -     } -      the      - }Wuyi  the classMyRunnable1Implementsrunnable{ -      Wu     PrivateShareData1 data1; -  About      PublicMyRunnable1 (ShareData1 data1) { $          This. data1 =data1; -     } -      - @Override A      Public voidrun () { + data1.decrement (); the     } - } $  the classMyRunnable2Implementsrunnable{ the      the     PrivateShareData1 data1; the  -      PublicMyRunnable2 (ShareData1 data1) { in          This. data1 =data1; the     } the      About @Override the      Public voidrun () { the data1.increment (); the     } + } -  the classShareData1/*implements Runnable*/{Bayi      the     Private intCount = 100; the      -     Private intj = 0; -      Public synchronized voidincrement () { theJ + +; the     } the      Public synchronized voidDecrement () { thej--; -     } the //@Override the //Public void Run () { the //While (true) {94 //count--; the //        } the //    } the     98}

7. How to share data among multiple threads-jdk5 multithreading

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.