Design 4 threads, where two threads add 1 to J at a time, and two threads each decrease by 1 for j

Source: Internet
Author: User

1  PackageZzg;2 3  Public classThreadTest {4 Private intJ;5  Public Static voidMain (string[] arge) {6ThreadTest T1 =Newthreadtest ();7inc inc = T1.NewInc ();8Dec dec = t1.NewDec ();9      for(inti=0;i<2;i++){TenThread T =NewThread (inc); One T.start (); At =NewThread (DEC); - T.start (); -     } the } - Private  synchronized voidInc () { -J + +; -System.out.println (Thread.CurrentThread (). GetName () + "-inc:" +j); + } - Private synchronized voidDec () { +j--; ASystem.out.println (Thread.CurrentThread (). GetName () + "-inc:" +j); at } - classIncImplementsrunnable{ -      Public  voidrun () { -          for(inti=0;i<100;i++){ - Inc (); -         } in     } - } to classDecImplementsrunnable{ +      Public voidrun () { -          for(inti=0;i<100;i++){ the Dec (); *         } $     }Panax Notoginseng } -}

Design 4 threads, where two threads add 1 to J at a time, and two threads each decrease by 1 for j

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.