Big Data Java Foundation Nineth day job

Source: Internet
Author: User
Tags message queue

The first question:/** * 1. Box maximum capacity 120g, (current honey amount  >=  maximum capacity, then bees do not pour honey, send notifications, waiting in the message queue)  * 2. Every time the Cubs eat 50g, (When the box honey quantity  < 50 does not eat, sends the notice, waits in the message queue)  * 3. Bees own pocket maximum 80g, bees produce 6g of honey every 1s, when pocket capacity  >=  50g then pour all the capacity into the box of the Cubs.  *///Bee Class Class bee extends thread{    public static final  int bag_max = 80;    public static final int fill _box = 50;//current capacity is greater than this value, it needs to be filled into the Bear's box     public String name;     public bigbox bigbox;    public int current_bag =  0;        public bee (String name,bigbox bigbox) {         this.name = name;         this.bigbox = bigbox;    }         p Ublic void run () {        while (true) {             synchronized (BigBox) {                 //first produce honey, then pour into                  if ((current_bag + 6)  <=  bag_max) {                     try{                         thread.sleep (;   )                  }catch ( Exception e) {};                     current_bag = current_bag + 6;                     system.out.println (name +  ": Produce 6g Honey! , current package capacity: " + current_bag + " g! ");                }                 if (current_bag  >= fill_box && bigbox. box_max >=  (bigbox.current_box + current_bag)) {                     bigbox.current_box =  bigbox.current_box + current_bag;                     system.out.println (name +  ": The capacity of the current package:"  + current_bag +  "G pour into the Bear's box! BoxSub-capacity: " + bigbox.current_box);                     current_bag = 0;                 }                 bigbox.notifyall ();                 try{                     bigbox.wait ();                 }catch (Exception e) {};             }         }    }}//Bear Class class smallbear extends thread{     public string Name;    public bigbox bigbox;    public smallbear ( String name,bigbox bigbox) {        this.name =  name;        this.bigbox = bigbox;    }     public void run () {        while (True ) {            synchronized (BigBox) {                 if (Bigbox.current_box  >= bigbox. Box_min) {                     bigbox.current_box = bigbox.current_box - bigbox. box_min;                     system. Out.println (name +  ": Consume 50g honey! , the capacity of the current box " + bigbox.current_box + " g! ");                }                 bigbox.notifyall ();                 try{                      bigbox.wait ();                 }catch (exception e) {};            }         }    }}//Cubs eat Honey Box class bigbox{     public static final int BOX_MAX = 120;     Public static final int&nBsp box_min = 50;    public int current_box = 0;} Class total{    public static void main (String[] args) {         bigbox bigbox = new bigbox ();                          bee bee001 = new bee ("Bee 001", BigBox);         bee bee002 = new bee ("Bee 002", BigBox);         bee bee003 = new bee ("Bee 003", BigBox);         bee bee004 = new bee ("Bee 004", BigBox);         bee bee005 = new bee ("Bee 005", BigBox);         bee001.start ();    &nBsp;    bee002.start ();         bee003.start ();         bee004.start ();         bee005.start ();                         SmallBear bear001 = new  Smallbear ("Bear 001", BigBox);        smallbear bear002 =  New smallbear ("Bear 002", BigBox);         bear001.start ();         bear002.start ();     }} The second question: 1, import java.util.list ;import java.util.arraylist;class equal{    public static void  Main (String[] args) {        //string pattern matching          string a_str =  "ABCABCDBBCD";        string b_str =  " BBCD ";                 string  short_str =  "";         string long_str =   "";         if (A_str.length ()  <= b_str.length ()) {             short_str = a_str;             long_str = b_str;         }else{             short_str = b_str;            long _str = a_str;        }         char short_arr[] = short_str.tochararray ();         char long_arr[]  = long_str.tochararray ();         int short_length  = short_arr.length;        int long_length =  long_arr.length;                 List<String> list_row = new ArrayList<String> ();                 for (Int i=0;i<short_ length;i++) {            for (Int j=0;j<long _length;j++) {                 if (Short_arr[i] == long_arr[j]) {                     int ii = i;                     int jj = j;                     int r  = 0;                     while (SHORT_ARR[II]&NBSP;==&NBSP;LONG_ARR[JJ]) {                          jj++;                         ii++;                         r++;         &nbSp;               if (ii >=  short_length | |  jj >= long_length) {                             break;                          }                     }                     list_row.add (long_str.substring (j,j+r));                 }             }        }        int length = 0;         String str_result =  "";         for (int  i=0;i<list_row.size (); i++) {            if (List_row.get (i). Length ()  > length) {                length =  list_row.get (i). Length ();                 str_result = list_row.get (i)  ;             }        }         system.out.println (Str_result);     }}2, Import java.util.list;import  java.util.arraylist;class equal{    public static&nBsp;void main (String[] args) {        string a =   "BBCD";        string b =  "ABCABCDBBCD";         List<String> list_a = new ArrayList< String> ();         list<string> list_b = new  ArrayList<String> (        list<string> list_); Row = new arraylist<string> ();                 for (Int i=0;i<a.length (); i++) {             for (Int j=i+1;j<=a.length (); j + +) {                 list_a.add (a.substring (i,j));             }        }                 for (Int i=0;i<b.length (); i++) {             for (Int j=i+1;j<=b.length (); j+ +) {                list_b.add ( B.substring (i,j));            }         }                 for (Int i=0;i<list_a.size (); i++) {             for (Int j=0;j<list_b.size (); j + +) {                 if (List_a.get (i). Equals (List_b.get (j))) {       &nBsp;             list_row.add (List_a.get (i));                 }             }        }                 int  length = 0;        string str_result =  "";         for (Int i=0;i<list_row.size (); i++) {             if (List_row.get (i). Length ()  > length) {                 length =  list_row.get (i). Length ();                 str_resulT = list_row.get (i)  ;            }         }                 system.out.println (str_result);     }} Question three: StringBuffer: Thread-safe. StringBuilder: Not thread safe. A: Performance is not the same, stringbuffer each access to determine whether the object is locked and unlocked, StringBuilder do not need to determine the object lock, StringBuilder performance is better. Question Fourth: Class auto{    public static void main (String[] args) {         /**         *  Boxing: Encapsulates the underlying type into a class.          *  Unpacking: Converts a class to the underlying type.          */        byte  b = 10;        Short s = 10;         integer i = 10;        long l = 10l;         Float f = 1.23f;         double d = 1.25d;        character c  =  ' A ';        boolean bool = true;         byte bb = b;         short ss = s;        int ii =  i;        long ll = l;         float ff = f;        double  dd = d;        char cc = c;         boolean base_bool = bool;    }} 


This article is from the "Forest Sensitive" blog, please be sure to keep this source http://senlinmin.blog.51cto.com/6400386/1774264

Big Data Java Foundation Nineth day job

Related Article

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.