感謝大家對IT十八掌大數據的支持,今天的作業如下
1.蜜蜂和熊的生產消費關係,熊在蜂蜜滿10斤吃掉。蜜蜂一次生產一斤蜂蜜,且蜜蜂生成一斤蜂蜜花費的時間是10s。
十隻蜜蜂和兩隻熊。
2.取出兩個字符串中最大的公共子串。
3.StringBuffer是線程安全的,StringBuilder不是線程安全。單線程訪問情況下,性能是否一致?
4.完成8中基本數據類包裝類的練習,完成自動拆裝箱操作。
--------------------------------------------------------------------------------------------------------
1.蜜蜂和熊的生產消費關係,熊在蜂蜜滿10斤吃掉。蜜蜂一次生產一斤蜂蜜,且蜜蜂生成一斤蜂蜜花費的時間是10s。
十隻蜜蜂和兩隻熊。
答:
/**
* 蜜蜂、熊的例子
*
*/
public class App {
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 ; //每生產5斤可以放入蜜罐
private static final int TIME = 10 ; //生產一斤花費10ms
private Box box ; //蜜罐子
private String name ;
public Bee(String name,Box box ){
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(); //通知熊來吃.
}
//不足remain
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 (Exception e) {
}
}
}
else{
bag ++ ;
System.out.println(name + ".bag = " + bag);
try {
Thread.sleep(10); //
}
catch (Exception e) {
}
}
}
}
}
public class Bear extends Thread{
private Box box ;
public static String name = "yyy";
//構造代碼塊
{
System.out.println("sss");
}
//構造函數
public Bear(Box box, String name) {
super();
this.box = box;
//this.name = name;
}
//
public void run() {
while(true){
synchronized (box) {
if(box.capacity == Box.MAX){
int tmp = box.capacity ;
box.capacity = 0 ;
System.out.println(name + " : 吃掉了 " + tmp);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
box.notifyAll();
}
else{
try {
box.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
/**
* 蜜罐子
*/
public class Box {
public static final int MAX = 30 ;
public int capacity = 0 ; //目前的量,其實爲0
}
2.取出兩個字符串中最大的公共子串。
答:
/**
* 取出兩個字符串中最大的公共子串
*
*/
public class LongestCommentString {
public static void main(String[] args) {
String str1 = "beijingchangpingshahe";
String str2 = "beijing@shahe";
String comment = getLongestCommentString(str1, str2);
System.out.println(comment);
}
private static String getLongestCommentString(String str1, String str2) {
List<String> str1Sub = new ArrayList<String>();
List<String> str2Sub = new ArrayList<String>();
List<String> listSame = new ArrayList<String>();
//str1包含的所有字符串
for (int i = 0; i <= str1.length(); i++) {
for (int j = i; j <= str1.length(); j++) {
str1Sub.add(str1.substring(i, j));
}
}
//str2包含的所有字符串
for (int i = 0; i <= str2.length(); i++) {
for (int j = i; j <= str2.length(); j++) {
str2Sub.add(str2.substring(i, j));
}
}
//包含相同的字符串
for (int i = 0; i < str1Sub.size(); i++) {
for (int j = 0; j < str2Sub.size(); j++) {
if (str1Sub.get(i).equals(str2Sub.get(j))) {
listSame.add(str1Sub.get(i));
}
}
}
//最大的公共子串
int maxId = 0;
int maxValue = 0;
for (int i = 0; i < listSame.size(); i++) {
if (listSame.get(i).length() > maxValue) {
maxId = i;
maxValue = listSame.get(i).length();
}
}
return listSame.get(maxId);
}
}
3.StringBuffer是線程安全的,StringBuilder不是線程安全。單線程訪問情況下,性能是否一致?
答:性能不一致,StringBuilder在每次訪問的時候不需要判斷對象鎖是否被佔用,性能更好效率更高;
4.完成8中基本數據類包裝類的練習,完成自動拆裝箱操作。
答:
// byte類型的自動裝箱與拆箱
Byte b1 = 1;
byte b2 = b1;
System.out.println("Byte " + (b1 == b2));
// Short類型的自動裝箱與拆箱
Short s1 = 1;
short s2 = s1;
System.out.println("Short " + (s1 == s2));
// Integer類型的自動裝箱與拆箱
Integer int1 = 1;
int int2 = int1;
System.out.println("Integer " + (int1 == int2));
// Long類型的自動裝箱與拆箱
Long long1 = 1L;
long long2 = long1;
System.out.println("Long " + (long1 == long2));
// Float類型的自動裝箱與拆箱
Float f1 = 3.1415f;
float f2 = f1;
System.out.println("Float " + (f1 == f2));
// Double類型的自動裝箱與拆箱
Double d1 = 3.1415d;
double d2 = d1;
System.out.println("Double " + (d1 == d2));
// 字符類型的自動裝箱與拆箱
Character c1 = 'a';
char c2 = c1;
System.out.println("Character" + (c1 == c2));
// Boolean類型的自動裝箱與拆箱
Boolean bool1 = false;
boolean bool2 = bool1;
System.out.println("Boolean " + (bool1 == bool2));