您的位置:首页 > 编程语言 > Java开发

IT十八掌作业_java基础第九天_多线程、自动拆装箱

2016-03-07 09:42 441 查看
感谢大家对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));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: