Java-Day7

Java 數(shù)組

1, 數(shù)組是有序數(shù)據(jù)的集合, 數(shù)組中的每個(gè)元素具有相同的數(shù)組名和下標(biāo)來做唯一標(biāo)識

數(shù)組聲明形式:

形式一: type arrayName[];

形式二: type[] arrayName;

為數(shù)組分配內(nèi)存空間:

1, 為數(shù)組分配內(nèi)存空間, 如果不分配內(nèi)存, 將不能訪問他的任何元素, 我們使用new關(guān)鍵字來為數(shù)組分配內(nèi)存空間

代碼:

int[] score; //數(shù)組的聲明

score = new int[3];//為數(shù)組開辟內(nèi)存空間, 實(shí)例化

for (int i = 0; i < score.length; i++) {//為數(shù)組賦值

score[i] = i*2+1;

}

for (int i = 0; i < score.length; i++) {//輸出數(shù)組的值

System.out.println(score[i]);

}

數(shù)組的靜態(tài)初始化

1, 數(shù)組初始化方式有兩種: 動(dòng)態(tài)初始化和靜態(tài)初始化

2, 靜態(tài)初始化: 在數(shù)組創(chuàng)建之初直接為其指定內(nèi)容

int score[] = {1,2,3};//聲明

代碼: 求一組數(shù)的最大值與最小值

int score[] = {43, 34,5, 66,12};//聲明

int max, min;

max = min = score[0];

for (int i = 0; i < score.length; i++) {

if (score[i] > max) {

max = score[i];

}

if (score[i] < min) {

min = score[i];

}

}

System.out.println("最大值"+max +" 最小值"+min);

代碼: 冒泡排序

int score[] = {12,45,23,10,300};

for (int i = 0; i < score.length-1; i++) {

for (int j = i + 1; j < score.length; j++) {

if (score[i]<score[j]){

int temp = score[i];

score[i] = score[j];

score[j] = temp;

}

}

}

for (int i = 0; i < score.length; i++) {

System.out.println(score[i]);

}

二維數(shù)組:

1, 如果把一維數(shù)組看成是線性圖形, 那么二維數(shù)組就是一個(gè)平面圖形

2 , 二維數(shù)組的聲明和一維數(shù)組類似, 內(nèi)存分配也是使用new關(guān)鍵字

3, 聲明與分配內(nèi)存: 聲明: type arrayName[][]; ?初始化: arrayName[][] = new type[行][列];

代碼: 二維數(shù)組靜態(tài)初始化并輸出

int score[][] = {{1,2},{3,4},{2,3,4},{4,5,6,7,7},{1,2}};

for (int i = 0; i < score.length; i++) {

for (int j = 0; j < score[i].length; j++) {

System.out.print(score[i][j] + " ");

}

System.out.println();

}

Java多線程

線程與進(jìn)程

1, 線程: 程序中單獨(dú)順序的控制流, ?線程本身依靠程序進(jìn)行運(yùn)行, 線程是程序中的順序控制流, 只能使用分配給程序的資源和環(huán)境

2, 進(jìn)程: 執(zhí)行中的程序 ?一個(gè)進(jìn)程可以包含一個(gè)或多個(gè)線程 ?, 一個(gè)進(jìn)程至少要包含一個(gè)線程

3,單線程: 程序中只存在一個(gè)線程, 實(shí)際上主方法就是一個(gè)主線程

4, 多線程: 多線程是在一個(gè)程序中運(yùn)行多個(gè)任務(wù), ?多線程的目的是更好的使用CPU資源

線程的實(shí)現(xiàn):

1, 在Java中,線程的實(shí)現(xiàn)由2中: (1):繼承Thread類 ? (2): 實(shí)現(xiàn)Runnable接口

2,Thread類: Thread類是在java.lang包中定義的, 繼承Thread類必須重寫run()方法

定義格式: class ?className extends Thread{run(){};}

線程的啟動(dòng)時(shí)通過start()方法

3, Runnable接口

runnable接口不存在啟動(dòng)的方法, 要通過thread類

MyRunnable run1 = new MyRunnable("A");

MyRunnable run2 = new MyRunnable("B");

//runnable接口不存在啟動(dòng)的方法, 要通過thread類

Thread t1 = new Thread(run1);

Thread t2 = new Thread(run2);

t1.start();

t2.start();

線程的狀態(tài):

1, 線程也有固定的操作狀態(tài)

創(chuàng)建狀態(tài): 準(zhǔn)備好了一個(gè)多線程的對象

就緒狀態(tài): 調(diào)用了start()方法, 等待CPU進(jìn)行調(diào)度

運(yùn)行狀態(tài): 執(zhí)行run()方法

阻塞狀態(tài): 暫時(shí)停止執(zhí)行, 可能將資源交給其他線程使用

終止?fàn)顟B(tài)(死亡狀態(tài)): 線程銷毀

線程的常用方法

1, 獲取線程名稱 ?getName()

2, 取得當(dāng)前線程對象 currentThread()

3, 判斷線程是否啟動(dòng) isAlive()

4, 線程的強(qiáng)制運(yùn)行 join()

5, 線程的休眠 ?sleep()

6, 線程的禮讓 ?yield();

代碼: 關(guān)于以上線程常用方法的案例

class RunnableDemo implements Runnable{

private String name;

public RunnableDemo(String name) {

this.name = name;

}

@Override

public void run() {

for (int i = 0; i < 50; i++) {

System.out.println(name+":"+i);

//線程的禮讓:

if (i == 10) {

System.out.println("禮讓:");

Thread.yield();

}

//線程的休眠, 單位是毫秒

// try {

// Thread.sleep(1000);

// System.out.println(name+":"+i);

// } catch (InterruptedException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }

//Thread.currentThread()獲取當(dāng)前線程

// System.out.println("當(dāng)前線程對象"+Thread.currentThread().getName());

}

}

}

public class ThreadDemo03? {

public static void main(String[] args) {

RunnableDemo r = new RunnableDemo("A");

RunnableDemo r2 = new RunnableDemo("B");

Thread t = new Thread(r);

Thread t2 = new Thread(r2);

//isAlive() 判斷線程是否啟動(dòng)

// System.out.println(t.isAlive());

t.start();

// System.out.println(t.isAlive());

t2.start();

//線程的強(qiáng)制運(yùn)行

// for (int i = 0; i < 50; i++) {

// if (i>10) {

// try {

// t.join();

// } catch (InterruptedException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }

// }

// System.out.println("主線程"+i);

// }

}

}

線程的優(yōu)先級

優(yōu)先級順序設(shè)置: 1-MIN_PRIORITY ?10-MAX_PRIORITY ? 5-NORM_PRIORITY ?如果什么都不設(shè)置默認(rèn)值是5

代碼:

class ThRun implements Runnable{

@Override

public void run() {

// TODO Auto-generated method stub

for (int i = 0; i < 3; i++) {

try {

Thread.sleep(1000);

//打印當(dāng)前線程名稱

System.out.println(Thread.currentThread().getName()+":"+i);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

public class ThreadDemo04 {

public static void main(String[] args) {

Thread t1 = new Thread(new ThRun(), "A");

Thread t2 = new Thread(new ThRun(), "B");

Thread t3 = new Thread(new ThRun(), "C");

//設(shè)置線程優(yōu)先級

t1.setPriority(Thread.MIN_PRIORITY);

t2.setPriority(Thread.NORM_PRIORITY);

t3.setPriority(Thread.MAX_PRIORITY);

t1.start();

t2.start();

t3.start();

}

}

線程同步

1, 同步代碼塊: ?在代碼塊上加上"synchronized"關(guān)鍵字, 則此代碼塊就稱為同步代碼塊

2, 同步代碼塊格式: ?synchronized(同步對象){ 需要同步的代碼塊;}

3, 同步方法 ?除了代碼塊可以同步, 方法也是可以同步的

4, 方法同步格式: synchronized void 方法名稱(){}

代碼:

class MyThreadDemo implements Runnable{

private int ticket = 5;

public void run() {

for (int i = 0; i < 10; i++) {

//同步代碼塊解決共享問題

// synchronized (this) {

// if (ticket > 0) {

// try {

// Thread.sleep(500);

// } catch (InterruptedException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }

// System.out.println("車票"+":"+ticket--);

// }

// }

}

}

//同步方法解決共享問題

public synchronized void tell(){

if (ticket > 0) {

try {

Thread.sleep(500);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("車票"+":"+ticket--);

}

}

}

public class ThreadDemo05 {

public static void main(String[] args) {

MyThreadDemo m = new MyThreadDemo();

Thread t1 = new Thread(m);

Thread t2 = new Thread(m);

Thread t3 = new Thread(m);

t1.start();

t2.start();

t3.start();

}

}

線程的生命周期:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,853評論 18 399
  • 一、 1、請用Java寫一個(gè)冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨(dú)云閱讀 1,510評論 0 6
  • 本文主要講了java中多線程的使用方法、線程同步、線程數(shù)據(jù)傳遞、線程狀態(tài)及相應(yīng)的一些線程函數(shù)用法、概述等。 首先講...
    李欣陽閱讀 2,602評論 1 15
  • 我見過你愛我時(shí)的樣子,才明白你是真的不再愛我了。 從剛開始在一起到現(xiàn)在一年多的時(shí)間,我們爭吵過N多次,也是從剛開始...
    獼猴tao閱讀 771評論 0 0
  • 【編者按】作者 Emil Soman,Rubyist,除此之外竟然同時(shí)也是藝術(shù)家,吉他手,Garden City ...
    OneAPM閱讀 312評論 0 1

友情鏈接更多精彩內(nèi)容