您的位置:首页 > 大数据

大数据第九天内容

2016-05-20 15:23 155 查看
笔记部分:

创建线程方式
-----------------
1.Thread
2.Runnable{public void run();}

class Man extends Person implements Runnable{
public void run(){
...
}
}

new Car().start();
new Thread(new Man()).start();

eclipse
---------------
alt + ///代码辅助
alt + 上箭头//向上移动一行
alt + 下箭头//向上移动一行
alt + shift + 上箭头 //向上复制一行
alt + shift + 下箭头 //向下复制一行
ctrl + D//删除一行

String
-----------------
1. == //判断是否是同一对象。判断对象的内存地址。
2. equals //是判断两个对象内容是否相同。
split(String reg); //按照指定的字符串进行拆分,返回值数组。
substring(int start); //取子串操作,指定起始索引之后的所有字符
substring(int start,int end);//取子串操作,指定起始索引和结束索引之间的全部字符,
//包含起始索引,不含结尾索引。[a,b)属于半开半闭区间。
byteByte //包装类
shortShort
intInteger
longLong
floatFloat
douleDouble
booleanBoolean
charCharacter
byte b = 127 ;
Byte b = new Byte(127);

StringBuffer
字符串缓冲区
StringBuffer是字符串变量,它的对象是可以扩充和修改的。
append方法可以追加字符串,字符,对象等

和String相比:当String方法执行Sting=String+i等情况时,实际上String对象是不可改变对象,因此不断地在创建对象,并字符串池中也不断增加,原来对像再被回收,String执行效率低。

StringBuffer中的方法是被synchronized修饰的,是同步的,线程安全的,同一时间只有一个对像可以执行,锁是执行时的当前StringBuffer对象

StringBuilder没有被synchronized修饰的,非同步的
性能不一致,StringBuilder在每次访问的时候不需要判断对像锁是否被占用,性能更好效率更高。

作业部分:

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

代码:
package com.work.ten;

class Bear extends Thread{

private FengMi fengmi;

private String bearname;

public Bear(FengMi fengmi,String  bearname){

this.fengmi=fengmi;

this.bearname=bearname;

}

public void run(){

while(true){

synchronized(fengmi){

if (fengmi.getFengmiWeight()<10) {

try {

fengmi.wait();

}
catch (Exception e){

}

}

else if(fengmi.getFengmiWeight()>=10&&fengmi.getFengmiWeight()%10==0){

fengmi.ChiFemgmi();

System.out.println("10 斤蜂蜜被"+bearname+"吃掉,剩余"+fengmi.getFengmiWeight()+"斤蜂蜜");

try{

fengmi.notifyAll();

}

catch(Exception e){

}

}

}

yield();

}

}

}

class FengMi {

private static int fengmiWeight=0; //蜜罐中蜂蜜数量

private  final int fengmiWeightMax=20; //蜜罐中蜂蜜数量上限

public  int getFengmiWeight() {
return fengmiWeight;
}

public int getFengmiWeightMax() {
return fengmiWeightMax;
}

public void CreateFemgmi() { //生产蜂蜜
this.fengmiWeight += 1;
}

public void ChiFemgmi() { //吃蜂蜜
this.fengmiWeight -=10;
}

}

class Bee extends Thread{

private FengMi fengmi;

private String  beename;

public Bee(FengMi fengmi,String beename){

this.fengmi=fengmi;

this.beename=beename;

}

public void run(){

int i=1;

while(true){

synchronized(fengmi){

if(fengmi.getFengmiWeight()<fengmi.getFengmiWeightMax()){

if(fengmi.getFengmiWeight()==0){

try{

fengmi.CreateFemgmi();

System.out.println("蜂蜜采集好了,目前一共"+fengmi.getFengmiWeight()+"斤蜂蜜");

fengmi.notifyAll();

Thread.sleep(10);

System.out.println("经过10ms,蜜蜂休息好了");

}

catch(Exception e){

}

}

else {

if (fengmi.getFengmiWeight()%10==0){

try{

fengmi.CreateFemgmi();

System.out.println("蜂蜜采集好了,目前一共"+fengmi.getFengmiWeight()+"斤蜂蜜");

fengmi.notifyAll();

Thread.sleep(10);

System.out.println("经过10ms,蜜蜂休息好了");

}

catch(Exception e){

}

}

else {

try{

fengmi.CreateFemgmi();

if(fengmi.getFengmiWeight()%10==0){

System.out.println("蜂蜜采集好了,目前一共"+fengmi.getFengmiWeight()+"斤蜂蜜,熊可以来采了");
}

else  System.out.println("蜂蜜采集好了,目前一共"+fengmi.getFengmiWeight()+"斤蜂蜜");

fengmi.notifyAll();

Thread.sleep(10);

System.out.println("经过10ms,蜜蜂休息好了");

}

catch(Exception e){

}

}

}
}

else {

System.out.println("蜂蜜满了");

try{

fengmi.wait();

}
catch(Exception e){

}

}

}

i++;

yield();

}

}

}

class 	BeeBearModel2{

public static void main(String[] args){

FengMi fengmi=new FengMi();

Bear bear1=new Bear(fengmi,"Bear1");

Bee bee1=new Bee(fengmi,"Bee1");

Bee bee2=new Bee(fengmi,"Bee2");

bear1.start();

bee1.start();

bee2.start();
}

}


作业1运行结果(部分):

蜂蜜采集好了,目前一共18斤蜂蜜
经过10ms,蜜蜂休息好了
蜂蜜采集好了,目前一共19斤蜂蜜
经过10ms,蜜蜂休息好了
蜂蜜采集好了,目前一共20斤蜂蜜,熊可以来采了
经过10ms,蜜蜂休息好了
10 斤蜂蜜被Bear1吃掉,剩余10斤蜂蜜
蜂蜜采集好了,目前一共11斤蜂蜜
经过10ms,蜜蜂休息好了
蜂蜜采集好了,目前一共12斤蜂蜜
经过10ms,蜜蜂休息好了
蜂蜜采集好了,目前一共13斤蜂蜜
经过10ms,蜜蜂休息好了

作业2
取出两个字符串中最大的公共子串。
package com.work.nine;

public class Find {

private String checkString1;

private String checkString2;

public String getCheckString1() {
return checkString1;
}

public void setCheckString1(String checkString1) {
this.checkString1 = checkString1;
}

public String getCheckString2() {
return checkString2;
}

public void setCheckString2(String checkString2) {
this.checkString2 = checkString2;
}

public Find(String checkString1,String checkString2){

this.checkString1=checkString1;

this.checkString2=checkString2;
}

public String FindMaxPublicString(boolean result){

if(checkString1==null||checkString2==null) return "";

else{

String temp=this.checkString2;

if(result){

temp=temp.toLowerCase();

String String1Lower=this.checkString1.toLowerCase();

String String2Lower=this.checkString2.toLowerCase();

if(String1Lower.equals(String2Lower)) return this.checkString1;

else{

while(!String1Lower.contains(temp)){

temp=temp.substring(0, temp.length()-1);
}

return temp;

}

}

else{

if(this.checkString1.equals(this.checkString2)) return this.checkString1;

else{

while(!this.checkString1.contains(temp)){

temp=temp.substring(0, temp.length()-1);
}

return temp;

}

}

}

}

}

package com.work.nine;

class MaxPublicStringDemo{

public static void main(String[] args){

Find find1=new Find("ABCDEFG","");

Find find2=new Find("ABCDEFG","ABCDEFG");

Find find3=new Find("ABCDEFG","HIGKLMN");

Find find4=new Find("ABCDEFG","AB");

Find find5=new Find("ABCDEFG","ab");

Find find11=new Find("ABCDEFG","AABBCCD");

System.out.println("最大的公共子串为"+find1.FindMaxPublicString(false));

System.out.println("最大的公共子串为"+find2.FindMaxPublicString(false));

System.out.println("最大的公共子串为"+find3.FindMaxPublicString(false));

System.out.println("最大的公共子串为"+find4.FindMaxPublicString(false));

System.out.println("最大的公共子串为"+find5.FindMaxPublicString(false));

System.out.println("最大的公共子串为"+find11.FindMaxPublicString(false));

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

Find find6=new Find("ABCDEFG","");

Find find7=new Find("ABCDEFG","ABCDEFG");

Find find8=new Find("ABCDEFG","AACCEEG");

Find find9=new Find("ABCDEFG","AB");

Find find10=new Find("ABCDEFG","abcdefg");

System.out.println("最大的公共子串为"+find6.FindMaxPublicString(true));

System.out.println("最大的公共子串为"+find7.FindMaxPublicString(true));

System.out.println("最大的公共子串为"+find8.FindMaxPublicString(true));

System.out.println("最大的公共子串为"+find9.FindMaxPublicString(true));

System.out.println("最大的公共子串为"+find10.FindMaxPublicString(true));

}

}
结果:

最大的公共子串为
最大的公共子串为ABCDEFG
最大的公共子串为
最大的公共子串为AB
最大的公共子串为
最大的公共子串为A
--------------------------------------------
最大的公共子串为
最大的公共子串为ABCDEFG
最大的公共子串为a
最大的公共子串为ab
最大的公共子串为ABCDEFG

作业3
StringBuffer是线程安全的,StringBuilder不是线程安全。单线程访问情况下,性能是否一致?
性能不一致,StringBuilder在每次访问的时候不需要判断对像锁是否被占用,性能更好效率更高。

作业4
完成8种基本数据类包装类的练习,完成自动拆装箱操作。
package com.work.nine;

public class chaizhuang {

public static void main(String[] args){

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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息