Write a program, four threads, where two threads each add 1 to J, while the other two threads reduce the J each time by 1, requiring the use of internal classes

Source: Internet
Author: User

/** *  @author  laishengfeng * @2014-8-27 *  @TODO   Write a program,   Four threads, of which two threads each add 1, *  to J each time a further two threads are reduced by 1 (requiring the use of an internal class thread) for each J  */public class test{  public  static void main (String[] args)  {  MyThread mt = new  MyThread ();   //mythread object   mt.new innerthread1 (). Start ();  mt.new  InnerThread1 (). Start ();   mt.new innerthread2 (). Start ();   mt.new innerthread2 (). Start ();  }}class mythread {  private int j = 100;   /* on the inner class of J-plus */  public class innerthread1 extends thread  {    public void run ()  {   while  (j < 120)     {    try {     thread.sleep (+);     }catch  (exception&NBSP;EX)  {    }    j++;    system.out.println (Thread.CurrentThread (). GetName () + "  " +j);   }   }  }  The inner class */   public class innerthread2 extends thread  {of  /* to J minus    public void run ()  {   while  (j > 80)     {    try {     thread.sleep (;  )   }catch  (Exception ex)  {    }    j--;     system.out.println (Thread.CurrentThread (). GetName () + "  " +j);    }    }  }}

Directly on the code is interested in mm I

Write a program, four threads, where two threads each add 1 to J, while the other two threads reduce the J each time by 1, requiring the use of internal classes

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.