1.蜜蜂和熊的生產消費關係,熊在蜂蜜滿10斤吃掉。蜜蜂一次生產一斤蜂蜜,且蜜蜂生成一斤蜂蜜花費的時間是10s。
十隻蜜蜂和兩隻熊。
2.取出兩個字符串中最大的公共子串。
3.StringBuffer是線程安全的,StringBuilder不是線程安全。單線程訪問情況下,性能是否一致?
4.完成8中基本數據類包裝類的練習,完成自動拆裝箱操作。
--------------------------------------------------------------------------------
//1.蜜蜂和熊的生產消費關係,熊在蜂蜜滿10斤吃掉。蜜蜂一次生產一斤蜂蜜,且蜜蜂生成一斤蜂蜜花費的時間是10s。
package com.it18zhang.homework;
public class BeeDemo {
public static void main(String[] args) {
Box box = new Box(); //蜜罐
Bee bee1 = new Bee("b-1", box);
Bee bee2 = new Bee("b-2", box);
Bee bee3 = new Bee("b-3", box);
Bee bee4 = new Bee("b-4", box);
Bee bee5 = new Bee("b-5", box);
Bee bee6 = new Bee("b-6", box);
Bee bee7 = new Bee("b-7", box);
Bee bee8 = new Bee("b-8", box);
Bee bee9 = new Bee("b-9", box);
Bee bee10 = new Bee("b-10", box);
Bear bear1 = new Bear("熊大",box);
// Bear bear2 = new Bear("熊二",box);
bee1.start();
bee2.start();
bee3.start();
bee4.start();
bee5.start();
bee6.start();
bee7.start();
bee8.start();
bee9.start();
bee10.start();
bear1.start();
// bear2.start();
}
}
public class Bee extends Thread{
int i = 0;
private int bag = 0;
private static final int BAG_MAX = 20;
// private static final int ONCE = 5;
// private static final int TIME = 10;
private Box box;
private String name;
public Bee(String name, Box box) {
super();
this.name = name;
this.box = box;
}
public void run(){
while(true){
if(bag >= 5){
synchronized(box){
int cap = box.capacity;
if(cap >= Box.MAX){
box.notifyAll();
}
else{
int remain = Box.MAX - cap;
if(bag >= remain){
box.capacity = Box.MAX ;
bag = bag - remain;
System.out.println(name + "添加了"+remain+",name.bag="+bag+"蜜罐有"+box.capacity);
box.notifyAll();
}
else{
box.capacity = box.capacity + bag;
System.out.println(name +"添加了"+ bag +",name.bag="+bag+"蜜罐有"+box.capacity);
bag = 0;
}
}
}
}
if(bag >= Bee.BAG_MAX){
synchronized(box){
try {
box.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else{
bag++;
System.out.println(name + ".bag="+bag);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class Bear extends Thread{
private Box box;
private String name = null;
{
System.out.println("sss");
}
public Bear(String name,Box box) {
super();
this.name = name;
this.box = box;
}
public void run()
{
while(true)
{
synchronized (box){
if(box.capacity == Box.MAX)
{
int tmp = box.capacity ;
box.capacity = 0;
System.out.println(name+"吃了"+tmp+"蜂蜜");
box.notifyAll();
}
else
{
try {
box.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
public class Box {
public static final int MAX = 30;
public int capacity = 0;
}
package com.it18zhang.homework;
/**
*
*
* @author Liubx
*
*2.取出兩個字符串中最大的公共子串
*思路:定義一個方法getMaxSubstring(str1,str2)
*sdafadf;asdfka
*fsadlfj substring
*| | y,z, 0~length()-0 1
*| | 0~length()-1 2
* | |
*| | 0~lenth()-2 3
*
*從兩個字符串長度較大的開始取子串比較,一旦發現相等的子串則該子串爲最大
*例如:str1.length()<str2.length()
*調用字符串的substring()方法
*/
public class MaxSubstring
{
public static void main(String[] args)
{
String str1 = "sldjfalsdfja;ldf";
String str2 = "jfalsdfja;";
System.out.println(getMaxSubstring(str1, str2));
System.out.println("----------------------------------");
System.out.println(getMaxSubstring(str2, str1));
}
public static String getMaxSubstring(String str1, String str2)
{
String max ="";
String min ="";
if(str1.length()>str2.length())
{
max = str1;
min = str2;
}
else
{
max = str2;
min = str1;
}
//假設str1>str2
for(int x=0; x<min.length(); x++)
{
/**x控制子串的大小
* y,z爲兩個夾板,都爲角標值,這裏也可以通過長度來控制,但是很容易混淆不清,使用角標可以很好的避免這個問題
* 每次確定y,z後也就是夾板大小後,就開始往後移動,停止的條件爲後夾板z到達字符串末尾也就是角標值爲str2.length()-1
* 但是我們使用substring(y,z)是左閉右開的區間,[y,z),所以z能取的角標最大值爲str2.length;
* 當z=str2.length()時,z++爲str2.length+1,所以循環條件只需設在此處即可。
*/
for(int y=0,z=min.length()-x;z<=min.length() ;y++,z++)
{
String temp = min.substring(y,z);
if(max.contains(temp))
return temp;
}
}
return "找不到相同子串";
}
}
3.StringBuffer是線程安全的,StringBuilder不是線程安全。單線程訪問情況下,性能是否一致?
答:性能不一致,線程安全意味着使用了同步,而同步需要加鎖,線程在訪問時都需要判斷鎖的狀態,較爲
消耗資源,在多線程編程中,要謹慎使用同步,只將需要同步的代碼同步。StringBuffer和StringBuilder
功能上相差不大,StringBuilder沒有使用同步,而StringBuffer使用了同步,所以StringBuffer性能會比StringBuilder低一些。
4.完成8中基本數據類包裝類的練習,完成自動拆裝箱操作。
/*
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
*/
//byte Byte
Byte b1 = 1; //自動裝箱
byte b2 = b1; //自動拆箱
System.out.println(b2)
// short Short
Short s1 = 23;
short s2 = s1;
System.out.println(s2);
// int Integer
Integer i1 = 24;
int i2 = i1;
System.out.println(i2);
// long Long
Long l1 = 312312;
long l2 = l1;
System.out.println(l2);
// float Float
Float f1 = 3.14f;
float f2 = f1;
System.out.println(f2);
// double Double
Double d1 = 3.1415926d;
double d2 = d1;
System.out.println(d2);
// char Character
Char c1 = 'f';
char c2 = c1;
System.out.println(c2);
// boolean Boolean
Boolean b1 = true;
boolean b2 = b1;
System.out.println(b2);