Interview questions, child threads 10 times child thread 2 executes 20 times with the main thread 100 times back and forth loop execution 50 times

Source: Internet
Author: User

For the interview, child thread 2 executes 10 sub-threads 2 execution 20 times   100 times with the main thread to loop the execution 50 times public class interview {public static  Void main (String[] args)  {final business b = new business ();new  Thread (New runnable () {@Overridepublic  void run ()  {for  (int i = 1;  i <= 50; i++)  {b.sub2 (i);}}). Start (); New thread (New runnable () {@Overridepublic  void run ()  {for  (int i  = 1; i <= 50; i++)  {b.sub3 (i);}}). Start ();for  (int i = 1; i <= 50; i++)  {b.main (i);}} class business{private int order=1; Lock lock = new reentrantlock (); Condition con1 = lock.newcondition (); Condition con2 = lock.newcondition (); Condition con3 = lock.newcondition ();p ublic void sub2 (int i) {lock.lock (); try  {while  (Order&NBSP;!=&NBSP;2)  {try {con2.await ();}  catch  (interruptedexception e)  {e.printstacktrace ();}} for  (int j = 1; j <= 10; j++)  {system.out.println ("sub2  thread is sequence of  " + j+ ". lamp of  " + i);} System.out.println ("/... ............"); order = 3;con3.signal ();  finally{lock.unlock ();}} PUBLIC&NBSP;VOID&NBSP;SUB3 (int i) {Lock.lock ();try {while  (order != 3)  {try  {con3.await ();}  catch  (interruptedexception e)  {e.printstacktrace ();}} for  (int j = 1; j <= 20; j++)  {system.out.println ("sub3  thread is sequence of  " + j+ ". lamp of  " + i);} System.out.println ("/... ............"); order = 1;con1.signal ();  finally{lock.unlock ();}} Public void main (int i) {LOck.lock ();try {while  (order != 1)  {try {con1.await ();}  catch  (interruptedexception e)  {e.printstacktrace ();}} for  (int j = 1; j <= 100; j++)  {system.out.println ("main  thread is sequence of  " + j+ ". lamp of  " + i);} order = 2; System.out.println ("......................  finally {lock.unlock ();}}}


This article comes from "either desperately, or roll back!" "Blog, be sure to keep this provenance http://jiangzuun2014.blog.51cto.com/8732469/1538354

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.