IT十八掌作业_java基础第九天_多线程自动拆装箱
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IT十八掌作业_java基础第九天_多线程自动拆装箱相关的知识,希望对你有一定的参考价值。
1.蜜蜂和熊的生产消费关系,熊在蜂蜜满10斤吃掉。蜜蜂一次生产一斤蜂蜜,且蜜蜂生成一斤蜂蜜花费的时间是10s。
十只蜜蜂和两只熊。
class Bee extends Thread{
private int bag;
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(Box box, String name) {
super();
this.box = box;
this.name = name;
}
public void run()
{
//满足放蜂蜜的条件
while (true) {
//如果不足 5
if(bag >= 0 && bag < 5){
bag ++;
try {
System.out.println(name + "加蜂蜜,数量" + bag);
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
}
if (bag >= 5) {
synchronized (box) {
int cap = box.capacity;
if (cap == Box.MAX) {
System.out.println("蜂蜜满了");
try {
box.wait(); //
} catch (Exception e) {
// TODO: handle exception
}
}//未满
else {
//剩余空间
int remain = Box.MAX - cap ;
if (bag >= remain) {
box.capacity = Box.MAX;
bag = bag - remain;
box.notifyAll();
}
else {
box.capacity = box.capacity + bag;
bag = 0;
System.out.println(name + "加蜂蜜,容器中的数量为" + box.capacity);
}
}
}
}
}
}
}
class Box {
public static final int MAX = 20;
public int capacity = 0 ;
}
class Bear extends Thread {
private Box box;
private String name;
public Bear(Box box, String name) {
super();
this.box = box;
this.name = name;
}
public void run()
{
while (true)
{
synchronized (box) {
if (box.capacity > 10)
{
System.out.println("总量为:"+box.capacity);
box.capacity = box.capacity - 10;
System.out.println(name + "吃了,剩余 "+box.capacity);
box.notifyAll();
}else {
try {
System.out.println(name + "等着急了");
box.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
class App2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Box box = new Box();
Bee b1 = new Bee(box,"b1");
Bee b2 = new Bee(box,"b2");
Bee b3 = new Bee(box,"b3");
Bee b4 = new Bee(box,"b4");
Bee b5 = new Bee(box,"b5");
Bee b6 = new Bee(box,"b6");
Bee b7 = new Bee(box,"b7");
Bee b8 = new Bee(box,"b8");
Bee b9 = new Bee(box,"b9");
Bee b10 = new Bee(box,"b10");
Bear bear = new Bear(box,"熊大");
Bear bear2 = new Bear(box,"熊二");
b1.start();
b2.start();
b3.start();
b4.start();
b5.start();
b6.start();
b7.start();
b8.start();
b9.start();
b10.start();
bear.start();
bear2.start();
}
}
2.取出两个字符串中最大的公共子串。
public class StringDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1 = "abcasdfasdfd";
String str2 = "a13asdfbc3dsadfasd23defsdfs";
int min = 0;
int max = 0;
int len1 = str1.length();
int len2 = str2.length();
//比较连个字符串的长度。
String minStr = len1 <= len2 ? str1 : str2;
String maxStr = len1 > len2 ? str1 : str2;
String targetStr = null;
//循环 从字符串长度小的最大的子串开始
for (int i = minStr.length(); i >= 1; i--) { // 取出子字符串
for (int j = 0; j < minStr.length() - i+1; j++) {
targetStr = minStr.substring(j, j + i);
// 比较子串 和 最大字符串是否匹配
for (int k = 0; k < maxStr.length() - targetStr.length()+1; k++) {
String tmp = maxStr.substring(k, k + targetStr.length());
System.out.println("循环第 " + k + "次");
if (maxStr.substring(k, k + targetStr.length()).equals(targetStr)) {
System.out.println(" 最大子字符串:" + targetStr);
System.exit(-1);
}
}
}
}
}
}
3.StringBuffer是线程安全的,StringBuilder不是线程安全。单线程访问情况下,性能是否一致?
在单线程访问的情况下,性能是一样的。
只有在多线程并发的情况下,StringBuilder的性能高于StringBuffer,
因为StringBuffer有加Synchronized关键字,每次只允许一个线程访问,而StringBuilder是可以同时访问的。
以上是关于IT十八掌作业_java基础第九天_多线程自动拆装箱的主要内容,如果未能解决你的问题,请参考以下文章