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);
本文出自 “菜鸟成就数据之路” 博客,谢绝转载!
原文地址:http://liubx.blog.51cto.com/11235064/1774267