Java基礎學習第九天

作業說明:

1.一共100個饅頭,40個工人,每個工人最多能吃3個饅頭。使用多線程輸出所有工人吃饅頭的情況。

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

多線程中的串行化機制 synchronized

class MantouDemo{

public static void main(String[] args){

//定義一個籃子饅頭對象

Basket basket = new Basket();

//定義40個工作對象

for (int i = 1 ; i <= 40 ; i++ )

{

new Worker("Work-" + i , basket).start();

}

 

}

}

 

//因爲是40個人一起吃,是多線程進行,所以繼承Thread多線程類

class Worker extends Thread{

private String name ;

private static int MAX = 3 ;  //定義工人可以吃饅頭的最大數

private int count ; //工人吃的數量

private Basket basket ;

 

public Worker (String name , Basket basket){

this.name = name ;

this.basket = basket ;

}

 

//定義吃的方法

public void run(){

while(true){

//1.判斷是否已經吃到最大數了

if(count >= MAX){

return ;

}

//2.取饅頭,返回的是取的數量

int no = basket.getMantou();

if(no == 0){  //說明沒有饅頭了

return ;

}

//3.取到饅頭了

count ++ ;

System.out.println(name + "號工人取到了第:"+ no + ":個饅頭") ;

}

}

 

}

 

//籃子

class Basket{

private int count = 100 ; //總饅頭數量

 

//加同步修飾的同步方法,以當前對象作爲鎖旗標

public synchronized int getMantou(){

int temp = count ;

count -- ;

return temp > 0 ? temp : 0 ;

}

}

==============================

3.用多線程模擬蜜蜂和熊的關係。

   蜜蜂是生產者,熊是消費者。蜜蜂生產蜂蜜是累加的過程,熊吃蜂蜜是批量(滿20吃掉)的過程。

   生產者和消費者之間使用通知方式告知對方。注意不能出現死鎖的現象。

100只蜜蜂,每次生產的蜂蜜是1.

熊吃蜂蜜是20(批量的情況)。

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

class BeeDemo{

public static void main(String[] args){

BeeBox box1 = new BeeBox();

 

for (int i = 1 ; i <= 100 ; i ++ ){

new Bee(i + "號蜜蜂",box1).start();

}

 

Bear x1 = new Bear("熊1" ,box1);

Bear x2 = new Bear("熊2" ,box1);

x1.start();

x2.start();

}

}

 

//蜜蜂類,繼承線程,因爲是100個蜜蜂同時生產

class Bee extends Thread{

private String name ;

private BeeBox beebox ;

 

public Bee(String name , BeeBox beebox){

this.name = name ;

this.beebox = beebox ;        

}

public void run(){

while(true){

int n = beebox.add();

System.out.println( name + "生產了第"+n+"個蜂蜜");

 

}

 

}

}

 

//兩隻熊同時吃蜂蜜,多線程併發

class Bear extends Thread{

private String name ;

private BeeBox beebox ;

 

public Bear(String name , BeeBox beebox){

this.name = name ;

this.beebox = beebox ;        

}

 

public void run(){

while(true){

beebox.remove();

//打印輸出哪隻熊吃掉了蜂蜜

System.out.println(name + "吃掉了個蜂蜜!");

}

}

}

 

//蜂蜜罐

class BeeBox{

//蜜罐可存放的最大量

private int MAX = 20 ;

//蜜罐當前存放量

private int count ;

 

//添加蜜的方法 每次+ 1

public synchronized int add(){

while(count >= MAX){

try{

this.notify();

this.wait();

}

catch(Exception e){

e.printStackTrace();

}

}

return count++;

}

 

//移除蜜的方法 每次移除最大量-20

public synchronized void remove(){

while (count < MAX){

try{

this.wait();

}

catch(Exception e){

e.printStackTrace();

}

}

count = 0 ;

this.notify();

}

}

 

 

Thread

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

併發。

在一個類已經有繼承類了,但需要實現多線程,就可以讓類實現 Runnable接口。

 

java.lang.Runnable

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

1.接口

2.這個接口中只有一個public void run() ;方法

3.供現有類實現線程功能。

4.使用Runnable對象創建線程

new Thread(Runnable r).start();

5.靜態同步方法是使用class作爲鎖。

6.非靜態同步方法是使用this(當前對象)作爲鎖。

 

class Car implements Runnable{

...

//靜態同步方法

static synchronized void xxx(){

...

}

}

 

new Thread(Runnable r).start();

 

s.start();是我們用來調run方法的

s.run();這個方法是由jvm調的,

 

線程安全性(同步synchronized是用來解決線程安全性問題的)

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

同步代碼塊

synchronized(Object){

...

}

 

非靜態同步方法 ,以對象爲鎖

public synchronized void xxx(){

...

}

 

靜態同步方法,以類作爲鎖

public static synchronized void xxx(){

...

}

 

IDE

---------

集成開發環境:

integrate development environment.

 

eclipse

-----------        

1.日蝕.

2.netbeans.

3.IDEA

4.borland Jbuilder

 

5.透視圖

perspective,

eclipse -> windows -> perspective ->

6.視圖

eclipse -> windows -> show view -> other -> all view

 

7.指定工作空間

eclipse --> file -> switch workspace -> other -> new -> ...

8.清理項目

    Project --->Clean--->Clean projcets selected below 表示把編譯後的 bin目錄下的內容全部清理,爲重新編譯做準備

 

9.alt + /

 

10.build path  構建路徑 就是指定類庫的路徑,如果需要使用其他的類庫,也是通過這個添加。

classpath

11.

 

線程狀態

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

1.NEW

尚未運行。

2.RUNNABLE

正在運行的狀態。

3.BLOCKED

等待檢視器的鎖定權。

synchronized(this){

 

}

4.WAITING

等待狀態(無限等待)

一個線程在等待另一個線程特定的操作。()

wait();

5.TIMED_WAITING

限時等待。

等待指定時間。

wait(xxx);

6.TERMINATED

線程退出。

 

7.Sleep

休眠。

 

String

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

1.java.lang.String

2.java.lang.Integer

3.常量

String name = "xxx" ;

name = "ddd";

for(i < 100000){

name = name + "" + i;

}

byte b = (int)1234;

String str = 123 + "" ; 將數字變成字符串

Object o = new Dog();

Dog d = (Dog)o ;

4.創建String區別

//1個對象 ,字符在串池中

String str1 = "abc";

//2對象 ,這裏使用了new 在堆分配了空間

String str2 = new String("abc");

 

5.split()

切割字符串,形成String數組.

"hello,,,world,".split(",");最後的,不生效.

 

6.調試 debug,找蟲子

 

7.String.substring();

子串.

//beginIndex : int

//endIndex : int

"hello world".substring(beginIndex,endIndex);

//子串是前包後不包

"hello world".substring(6,10);

 

編碼表

-----------

所有的文字在計算機中都是用數字表示的,對應到相應的編碼表中,就表示對應的文字了。

1.ascii,美國標準交換碼

7位表示

2.iso-8859-1

歐洲碼錶,一個byte的8表示。

3.GB2312

簡體中文。2個字節表示.

4.unicode   例:\uFFFF 一個F4位,FF代表1個字節

國際標準碼.包含各國文字。使用2個字節

JAVA在計算機中的字符內部就是用這個形式存儲的

5.UTF-8

最多使用3個byte表示字符。

 

char c = 97 ;//int

一個字符的賦值可以通過 數值、字符、unicode來賦值

char a = 97 ;  //用數值

a = 'a' ;           //直接使用字符

a = '\u0063';  //使用unicode碼(unicode碼是4位的)

 

作業

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

1.StringUtil.

substring(int beginIndex,int length);

2.

3.

4.com.it18zhang.stringdemo.App


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章