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();
}
}
線程的生命周期:
