1.蜜蜂和熊的生产消费关系,熊在蜂蜜满10斤吃掉。蜜蜂一次生产一斤蜂蜜,且蜜蜂生成一斤蜂蜜花费的时间是10s。
十只蜜蜂和两只熊。
2.取出两个字符串中最大的公共子串。
3.StringBuffer是线程安全的,StringBuilder不是线程安全。单线程访问情况下,性能是否一致?
4.完成8中基本数据类包装类的练习,完成自动拆装箱操作。
--------------------------------------------------------------------------------------------------
class Demo
{
public static void main(String[] args)
{
Box box=new Box();
Bee bee1=new Bee(box);
Bee bee2=new Bee(box);
Bee bee3=new Bee(box);
Bee bee4=new Bee(box);
Bee bee5=new Bee(box);
Bee bee6=new Bee(box);
Bee bee7=new Bee(box);
Bee bee8=new Bee(box);
Bee bee9=new Bee(box);
Bee bee10=new Bee(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();
}
}
/**
1.蜜蜂和熊的生产消费关系,熊在蜂蜜满10斤吃掉。蜜蜂一次生产一斤蜂蜜,且蜜蜂生成一斤蜂蜜花费的时间是10s。
十只蜜蜂和两只熊。
2.取出两个字符串中最大的公共子串。
3.StringBuffer是线程安全的,StringBuilder不是线程安全。单线程访问情况下,性能是否一致?
4.完成8中基本数据类包装类的练习,完成自动拆装箱操作。
*/
public class Box
{
private final int Max=100;
private int Size=0;
public synchronized void add(){
if(Size<Max){
Size++;
Thread.sleep(10000);
System.out.println("蜂蜜增加到了"+Size+"个");
this.notifyAll();
//如果成了通知别的参与者来取锁
//熊一定被唤醒
}else{
this.wait();
//如果大于等于100当前线程休眠
//this.notify();
//this.wait();
}
}
public synchronized void eat(){
if(Size>=Max){
Size=0;
System.out.println("吃完了蜂蜜");
this.notifyAll();
//唤醒所有蜜蜂可以采蜜了
}else{
this.wait();
//如果没有到100个熊就等待,将自己加入等待Box的队列
}
}
}
public class Bear extends Thread
{
private Box box;
public Bear (Box box){
this.box=box;
}
public void run(){
while(true){
box.eat();
}
}
}
public class Bee extends Thread
{
private Box box;
//蜜蜂有一个Box只有大家的Box是同一个的时候线程才同步
public Bee (Box box){
this.box=box;
}
public void run(){
while(true){
box.add();
}
}
}
class StringDemo
{
public String MaxCommon(String A,String B){
String min=A.length()>B.length()?B:A;
String max=A.equals(min)?B:A;
String result="";
for(int i=min.length();i>0;i--){
//遍历小数组的length()大小
for(int k=0,j=i;j<min.length();k++,j++){
result=min.subString(k,k+j);
//保留结尾index,便于查出是否越界
if(max.indexof(result)!=-1){
return result;
}//首尾都移动更容易控制数组越界的问题
}
}
return result;
}
StringBuilder在每次访问的时候不需要判断对象锁是否被占用,性能更好
Integer i=new Integer(2)
int j=10;
i=j;//自动装箱
j=i;//自动拆箱
原文:http://11102728.blog.51cto.com/11092728/1748969