Multi-threaded Case _ Loop Print _ Design 4 threads ...

Source: Internet
Author: User

Requirements: Design 4 threads, of which two threads increase each time to I 1, the other two threads to i reduce each time 1, write out the program.

In the following procedure, Numofprint represents I in the topic

In order to resolve the issue of the other party's awakening. Multiple monitor objects (condition objects) can be created with a single lock .

1 Importjava.util.concurrent.locks.Condition;2 ImportJava.util.concurrent.locks.Lock;3 ImportJava.util.concurrent.locks.ReentrantLock;4 5 /**6  * @authorMa Jian 11:45:39 January 19, 2018 morning7  */8  Public classThreadTest6 {9      Public Static voidMain (string[] args) {Ten         FinalNumberincreate ni =Newnumberincreate (); One         NewThread (NewRunnable () { A  - @Override -              Public voidrun () { the                  for(inti = 0; I < 5; i++) { - ni.num (); -                 } -  +             } -}, "A"). Start (); +         NewThread (NewRunnable () { A  at @Override -              Public voidrun () { -                  for(inti = 0; I < 5; i++) { - ni.num2 (); -                 } -  in             } -}, "B"). Start (); to         NewThread (NewRunnable () { +  - @Override the              Public voidrun () { *                  for(inti = 0; I < 5; i++) { $ ni.num3 ();Panax Notoginseng                 } -  the             } +}, "C"). Start (); A         NewThread (NewRunnable () { the  + @Override -              Public voidrun () { $                  for(inti = 0; I < 5; i++) { $ ni.num4 (); -                 } -  the             } -}, "D"). Start ();Wuyi     } the  - } Wu  - classNumberincreate { About     Private intnum = 1;//the markup for the currently executing thread $     Private intNumofprint = 0;//the printed number -     PrivateLock lock =NewReentrantlock (); -Condition Condition =lock.newcondition (); -Condition Condition2 =lock.newcondition (); ACondition Condition3 =lock.newcondition (); +Condition Condition4 =lock.newcondition (); the  -      Public voidnum () { $ Lock.lock (); the         Try { the             if(num! = 1) { the condition.await (); the             } -System.out.println ("numofprint==" + numofprint++); innum = 2; the condition2.signal (); the}Catch(Exception e) { About}finally { the Lock.unlock (); the         } the  +     } -  the      Public voidnum2 () {Bayi Lock.lock (); the         Try { the             if(Num! = 2) { - condition2.await (); -             } theSystem.out.println ("numofprint==" + numofprint++); thenum = 3; the condition3.signal (); the}Catch(Exception e) { -}finally { the Lock.unlock (); the         } the 94     } the  the      Public voidnum3 () { the Lock.lock ();98         Try { About             if(Num! = 3) { - condition3.await ();101             }102System.out.println ("numofprint==" + numofprint--);103num = 4;104 condition4.signal (); the}Catch(Exception e) {106}finally {107 Lock.unlock ();108         }109  the     }111  the      Public voidnum4 () {113 Lock.lock (); the         Try { the             if(Num! = 4) { the condition4.await ();117             }118System.out.println ("numofprint==" + numofprint--);119num = 1; - condition.signal ();121}Catch(Exception e) {122}finally {123 Lock.unlock ();124         } the 126     }127}

Multi-threaded Case _ Loop Print _ Design 4 threads ...

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.