java拆装箱_java基础第九天_多线程、自动拆装箱

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.取出两个字符串中最大的公共子串。

思路:把字符串使用循环方法放到list里面,循环是把字符串每一段截取都取出来,最后比较list里面内容,取出最长的字串。

/**

* 取出两个字符串中最大的公共子串

*

*/

import java.util.List;

import java.util.ArrayList;

public class LongestCommentString {

public static void main(String[] args) {

String str1 = "1beijingchangpingshahe";

String str2 = "beijing@shahe";

String comment = getLongestCommentString(str1, str2);

System.out.println(comment);

}

private static String getLongestCommentString(String str1, String str2) {

List str1Sub = new ArrayList();

List str2Sub = new ArrayList();

List listSame = new ArrayList();

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

问题:练习有点少,

心得:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值