1.蜜蜂和熊的生产消费关系,熊在蜂蜜满10斤吃掉。蜜蜂一次生产一斤蜂蜜,且蜜蜂生成一斤蜂蜜花费的时间是10ms。

  十只蜜蜂和两只熊。

2.取出两个字符串中最大的公共子串。

3.StringBuffer是线程安全的,StringBuilder不是线程安全。单线程访问情况下,性能是否一致?

4.完成8中基本数据类包装类的练习,完成自动拆装箱操作。

------------------------------作业1---------------------------------------

package 作业一;

//1.密封盒熊的生产消费关系,想在密封满10斤的时候吃掉。密封一次生产一斤密封,且生产一斤蜂蜜花费的时间是10s

//十只蜜蜂,2只熊

//注意:生产了一个流程不动了,不一定是死锁了,不要忘记是写在循环条件里面

public class App {

public static void main(String[] args) {

Box box =new Box();

Bee bee1=new Bee(box,"b-1");

Bee bee2=new Bee(box,"b-2");

Bee bee3=new Bee(box,"b-3");

Bee bee4=new Bee(box,"b-4");

Bee bee5=new Bee(box,"b-5");

Bee bee6=new Bee(box,"b-6");

Bee bee7=new Bee(box,"b-7");

Bee bee8=new Bee(box,"b-8");

Bee bee9=new Bee(box,"b-9");

Bee bee10=new Bee(box,"b-10");

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();

}

}

--------



package 作业一;


public class Bear extends Thread {

private Box box;//持有蜜罐---私有的,但这里box和前面蜜蜂里面定义的有关系么?

public static String name="yyy";

//构造代码块

{

System.out.println("sss");

}

//构造函数

public Bear(Box box,String name){

super();

this.box=box;

this.name=name;

}

//run函数

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) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

box.notifyAll();

}

else{

try {

box.wait();//没有满的话等待

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

}

}

}

}

-------

package 作业一;

//同步代码块比同步方法容易理解一些

//蜜蜂

//蜜蜂和熊的生产消费关系,熊在蜂蜜满30斤吃掉。蜜蜂一次生产一斤,且一斤花费10秒(次要)

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斤可以放入蜜罐,是放5斤还是放多少的话还要取决于蜜罐的可用容量,自己袋子的量

private static final int TIME=10;//生产一斤花费的时间10s

private Box box;//蜜罐子的参数接收组进来,----取创建一个蜜罐

private String name;

//构造

public Bee(Box box, String name) {

super();

this.box = box;

this.name = name;

}

//run函数

public void run(){

while(true){

if(bag>=5){

//如果不足5,箱蜜罐放蜂蜜

synchronized (box) {//---在这里运行的条件是bag>=5--生产5斤可以放入蜜罐,是放5斤还是放多少的话还要取决于蜜罐的可用容量,自己袋子的量

//取出当前蜜罐容量boox.capcity

int cap=box.capacity;//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{//小于remain

box.capacity=box.capacity+bag;

System.out.println(name+"添加了"+bag+",name.bag="+bag+",蜜蜂有"+box.capacity);

bag=0;

}

}

}

}

else{//对应if(bag>=5)那里-----bag<5的情况

bag++;

System.out.println(name+".bag="+bag);

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

}

}

}


----------------------

package 作业一;

//蜜罐子

public class Box {

public static final int MAX=30;//最大容量30

public int capacity=0;//当前容量

}






【--------------------------------作业2-------------------------------------------】

package 作业2;

//ctrl+shift+o自动导包

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;


//取出两个字符串中最大的公共子串---------就是两个字符串里面连续字符相同最多的部分


//思路

//1.把字符串"abcd"变成a,ab,abc,abcd放进集合里面---另外一个字符串“bcde”变成b,bc,bcd,bcde放进集合里面---//对应str1,str2包含的所有字符串

//3.然后遍历比较集合里面的每个字符串,相同的字符串加入到相同的集合列表里面--对应方法//包含相同的字符串那里

//4.然后遍历集合每个元素的长度,取出长度最长的----对应//最大的公共子串那里


//不过这个也有缺陷,如"abcdfff";"bcdefff";bcd和fff的长度都是一样的,打出来确实bcd


public class LongestCommentString2 {



public static void main(String[] args) {


String str1 = "abcdfff";//"beijinggg1111ch@angpingshahe";

        String str2 = "bcdefff";//"beijinggggg@shahe";

        //char Str2[] = {'b', 'e', 'i','j'......};//效果其实就是这样



        //调用下面的方法

        String comment = getLongestCommentString(str1, str2);

        System.out.println(comment);

        

//        测试

        List<String> str1Sub = new ArrayList<String>();

//        System.out.println(str1.substring(1, 2));

        

        for (int i = 0; i <= str1.length(); i++) {

            for (int j = i; j <= str1.length(); j++) {

//                System.out.println(str1.substring(i, j));

                str1Sub.add(str1.substring(i, j));

            }

        }

        

        //普通型遍历集合

//        System.out.println("-------------------");

//        for(int i=0;i<str1Sub.size();i++){

//        System.out.print(str1Sub.get(i));

//        }

        

}


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++) {//str1还是字符串

            for (int j = i; j <= str1.length(); j++) {

                str1Sub.add(str1.substring(i, j));

                //str1Sub.add(e)向列表str1Sub的尾部追加指定的元素

                

                //字符串String.substring方法

//                public String substring(int beginIndex,

//                        int endIndex)返回一个新字符串,它是此字符串的一个子字符串。

//                该子字符串从指定的 beginIndex 处开始,一直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。

            }

        }


        //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-------------------------------------------】

3.StringBuffer是线程安全的,StringBuilder不是线程安全。单线程访问情况下,性能是否一致?


答:性能不一致,StringBuilder在每次访问的时候不需要判断对象锁是否被占用,性能更好效率更高。


【--------------------------------作业4-------------------------------------------】

package 作业2;


//完成8种基本数据类包装类的练习,完成自动拆装箱操作。

//答:


public class autoUpUi4{

public static void main(String[] args) {

// byte类型的自动装箱与拆箱

        Byte b1 = 1;//对应的包装类,自动拆箱

        Byte b3 =new Byte("11");

//        参数:

//        s - 要转换成 Byte 的 String 


        System.out.println(b3);

        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;

        Integer int3=new Integer(3);

        int int2 = int1;

        System.out.println("Integer " + (int1 == int2));


        // Long类型的自动装箱与拆箱

        Long long1 = 1L;

        Long long3=new Long(2);

        System.out.println(long3+"--------");

        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 bool3=new Boolean(false);

        bool3=new Boolean("false");

        boolean bool2 = bool1;

        System.out.println("Boolean " + (bool1 == bool2));

}

}