第一题:
/**
* 1.盒子最大容量120g,(当前蜂蜜量 >= 最大容量时,则蜜蜂不往里倒蜂蜜,发送通知,在消息队列等待)
* 2.小熊每一次吃50g,(当盒子蜂蜜量 < 50则不吃,发送通知,在消息队列等待)
* 3.蜜蜂自己的口袋最大量80g,蜜蜂每1s生产6g蜂蜜,当口袋容量 >= 50g时则把所有容量倒入小熊的盒子。
*/
//蜜蜂类
class Bee extends Thread{
public static final int BAG_MAX = 80;
public static final int fill_box = 50;//当前容量大于这个值,就需要填充到小熊的盒子里面
public String name;
public BigBox bigbox;
public int current_bag = 0;
public Bee(String name,BigBox bigbox){
this.name = name;
this.bigbox = bigbox;
}
public void run(){
while(true){
synchronized(bigbox){
//先产蜜,再倒入
if((current_bag + 6) <= BAG_MAX){
try{
Thread.sleep(1000);
}catch(Exception e){};
current_bag = current_bag + 6;
System.out.println(name + ":生产6g蜂蜜!,当前包的容量:" + 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 + ":将当前包的容量:" + current_bag + "g倒入熊的盒子!,盒子容量:" + bigbox.current_box);
current_bag = 0;
}
bigbox.notifyAll();
try{
bigbox.wait();
}catch(Exception e){};
}
}
}
}
//小熊类
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 + ":消费50g蜂蜜!,当前盒子的容量" + bigbox.current_box + "g!");
}
bigbox.notifyAll();
try{
bigbox.wait();
}catch(Exception e){};
}
}
}
}
//小熊吃蜂蜜的盒子
class BigBox{
public static final int BOX_MAX = 120;
public static final int BOX_MIN = 50;
public int current_box = 0;
}
class Total{
public static void main(String[] args){
BigBox bigbox = new BigBox();
Bee bee001 = new Bee("蜜蜂001",bigbox);
Bee bee002 = new Bee("蜜蜂002",bigbox);
Bee bee003 = new Bee("蜜蜂003",bigbox);
Bee bee004 = new Bee("蜜蜂004",bigbox);
Bee bee005 = new Bee("蜜蜂005",bigbox);
bee001.start();
bee002.start();
bee003.start();
bee004.start();
bee005.start();
SmallBear bear001 = new SmallBear("熊001",bigbox);
SmallBear bear002 = new SmallBear("熊002",bigbox);
bear001.start();
bear002.start();
}
}
第二题:
1、
import java.util.List;
import java.util.ArrayList;
class Equal{
public static void main(String[] args){
//字符串的模式匹配
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] == long_arr[jj]){
jj++;
ii++;
r++;
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 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))){
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);
}
}
第三题:
StringBuffer:是线程安全的。
StringBuilder:不是线程安全。
答:性能不一样,StringBuffer每次访问判断对象是否已锁和未锁,StringBuilder不需要判断对象锁,
则StringBuilder性能更好一些。
第四题:
class Auto{
public static void main(String[] args){
/**
* 装箱:把基础类型封装成一个类。
* 拆箱:把类转换成基础类型。
*/
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;
}
}
转载于:https://blog.51cto.com/senlinmin/1774264