日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java高级教程_Java高级教程02

發(fā)布時間:2025/3/12 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java高级教程_Java高级教程02 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.Java線程

1.1. 多線程和多進程

多進程:操作系統(tǒng)能夠同時進行多個任務: 每個app(word,播放器,瀏覽器)可以同時運行

多線程:同一應用程序中喲多個順序流同時執(zhí)行

線程是進程中的一部分

1.2. 線程的執(zhí)行過程:

主要過程:

多線程執(zhí)行的搶CPU是不規(guī)律的,由虛擬機分配

1.3. 創(chuàng)建線程的方法

(1). 方法1:通過run()

定義一個線程類,它繼承類Thread并重寫其中的run()方法,方法run()成為線程體

由于Java只支持單繼承,用這種方法定義的類不能繼承其他類

class ThreadOne extends Thread{

public void run(){

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

System.out.println("thread one--->" + i);

}

}

}

class ThreadTwo extends Thread{

public void run(){

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

System.out.println("thread two--->" + i);

}

}

}

class Test{

public static void main(String[] args){

// 生成線程類的對象

ThreadOne fo = new ThreadOne();

ThreadTwo ft = new ThreadTwo();

// 啟動線程---> 進入Runnable狀態(tài)---->準備搶CPU

fo.start();

ft.start();

}

}

(2). 方法2: 復寫Runnable接口(推薦)

提供一個實現(xiàn)接口Runnable的類作為線程的目標對象,在初始化一個Thread類或者Thread子類的線程對象時,把目標對象傳遞給這個線程實體,由該目標對象提供線程體

class RunnableImpl implements Runnable{

public void run(){

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

System.out.println("thread two--->" + i);

}

}

}

class Test{

public static void main(String[] args){

//生成一個Runnable接口實現(xiàn)類的對象

RunnableImpl ri = new RunnableImpl();

//生成一個Thread對象,并將Runnable接口實現(xiàn)類的對象作為參數(shù)傳遞給該Thread對象

Thread t = new Thread(ri);

// 通知thread執(zhí)行

t.start();

}

}

1.4. 線程的簡單控制

中斷線程

Thread.sleep():先睡眠,然后繼續(xù)進入就緒狀態(tài),準備搶CPU----記得拋出異常哦,親

Thread.yield():讓出CPU,然后繼續(xù)進入就緒狀態(tài),準備搶CPU

設置線程的優(yōu)先級:

getPriority(): 獲取優(yōu)先級

setPriority(): 設置優(yōu)先級(1-10)

2. Java線程同步synchronized

2.1. 多線程數(shù)據(jù)安全以及synchronized的使用

多線程共用同一份數(shù)據(jù)的時候,會出問題

class MyThread implements Runnable{

int i = 100;

public void run(){

while (true){

// 使用synchronized構造同步代碼塊---this為同步鎖

synchronized(this){

// Thread.currentThread()獲取當前代碼正在哪個線程中運行

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

i = i -1;

Thread.yield();

if(i<0){

break;

}

}

}

}

}

class Test{

public static void main(String[] args){

MyThread myThread = new MyThread();

// 生成兩個

Thread t1 = new Thread(myThread);

Thread t2 = new Thread(myThread);

t1.setName("thread a");

t2.setName("thread b");

// t1先獲得鎖,運行,t2等待

// t2然后獲得鎖,運行,t1等待

t1.start();

t2.start();

}

}

2.2. 深入synchronized關鍵字

同步鎖不是鎖的代碼塊,鎖的是this, 只要一個對象獲得同步鎖,這個對象其他也含有同步鎖的代碼都不能執(zhí)行,只能釋放后才能執(zhí)行

沒有同步鎖的代碼塊跟同步鎖無關,會繼續(xù)執(zhí)行,沒有影響

class Service {

public void fun1(){

synchronized(this){

try{

Thread.sleep(3*1000)

}

catch(Exception e){

System.out.println(e)

}

System.out.println("fun1")

}

}

public void fun2(){

synchronized(this){

System.out.println("fun2")

}

}

}

class MyThread1 implments Runnable{

private Service service;

public MyThread1(Service serivce){

this.serivce = serivce;

}

public void run(){

service.fun1();

}

}

class MyThread2 implments Runnable{

private Service service;

public MyThread2(Service serivce){

this.serivce = serivce;

}

public void run(){

service.fun2();

}

}

class Test{

public static void main(String[] args){

Service service = new Service();

Thread t1 = new Thread(new MyThread1(service));

Thread t2 = new Thread(new MyThread2(service));

}

}

2.3. 同步方法

同步方法鎖住的是this

class Service {

// 同步方法只需要在方法名前加入synchronized即可

public synchronized void fun1(){

try{

Thread.sleep(3*1000)

}

catch(Exception e){

System.out.println(e)

}

System.out.println("fun1")

}

public void fun2(){

synchronized(this){

System.out.println("fun2")

}

}

}

3. Java的數(shù)組和類集框架

用于儲存一些列相同數(shù)據(jù)類型的容器

3.1. 數(shù)組類型

數(shù)組長度一旦聲明,不可更改

class Test{

public static void main(String[] args){

// 一維數(shù)組的靜態(tài)聲明法

int[] arr = {1,2,5,7,8,10};

arr[3] = 10; // 設置數(shù)組元素為新的值

// 打印一維數(shù)組元素

for (int i=0; i

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

}

// 一維數(shù)組的動態(tài)聲明法

int[] arr = new int[10]; // 初始化為0

// 二位數(shù)組的靜態(tài)聲明法

int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};

arr[1][1]; // = 5

// 二位數(shù)組的動態(tài)聲明法

int[][] arr = new int[3][5];

// 打印二位數(shù)組

for (int i=0; i

for (int j=0; i

System.out.println(arr[i][j]);

}

}

}

}

3.2. Java的類集框架

1. 類集框架的定義和種類,以及基礎結構

類集框架是一組類和結構,位于java.util包中,主要用于儲存和管理對象,主要分為三大類:

集合(Set): 對象不按照特定的方式排序,并且沒有重復對象

列表(List): 對象按照索引位置排序,可以有重復對象

映射(dictionary): 通過鍵-值對儲存(key-value)

類集框架的主體結構

2. ArrayList列表的使用

import java.util.List;

import java.util.ArrayList;

public class Test{

public static void main(String[] args){

// arraylist的長度可以自動擴展,跟數(shù)組有區(qū)別

// 聲明arraylist只能存String類型

ArrayList arraylist = new ArrayList();

// 向arraylist數(shù)組添加對象

arraylist.add("a");

arraylist.add("b");

// 從arraylist取對象

String s = arraylist.get(1);

// 打印arraylist數(shù)據(jù)

for(int i=0; i

String s = arraylist.get(1);

System.out.println(s);

}

// 刪除arraylist數(shù)據(jù)

arraylist(1);

}

}

3. Collection和Iterator接口

Iterator最高層---hasNext() + Next()

Collection接口是Iteator的子接口

Set是Collection接口的子接口

HashSet是Set的實現(xiàn)類

Iterator

Iterator

(1) Collection接口

方法

含義

boolean add(Object 0)

向集合添加對象

void clear()

刪除集合的所有對象

boolean isEmpty()

判斷集合是否為空

remove(Object o)

從集合中刪除一個對象的引用

int size()

返回集合中元素的數(shù)組

4.Set和HashSet用法(Collection的實現(xiàn)類)

import java.util.Set;

import java.util.HashSet;

public class Test{

public static void main(String[] args){

HashSet hashset = new HashSet();

Set set = hashset;

boolean b1 = set.isEmpty();

set.add("a");

set.add("b");

set.add("c");

set.add("a"); // 重復元素會忽略

int a = set.size();

set.remove(a);

set.clear();

// 集合取數(shù)據(jù)---通過迭代器Iterator

// 調用Set對象的Iterator方法,會生成一個迭代器對象,該對象用于遍歷整個Set

Iterator it = set.iterator();

while(it.hasNext()){

//取出元素,并將指針向后面挪一位

String s = it.next();

System.out.println(s);

}

}

}

5. Map和HashMap的使用方法

Map

import java.util.Map;

import java.util.HashMap;

public class Test{

public static void main(String[] args){

// 創(chuàng)建hashmap對象,并定義鍵值對類型

HashMap hasMap = new HashMap();

Map map = hasMap;

map.put("1","a");

map.put("2","b");

map.put("3","c");

map.put("3","e"); // 將會覆蓋上面的鍵值對

int i = map.size();

String s = map.get("3");

}

}

4. equals函數(shù)的使用方法

4.1. equals的作用

==的作用:

基本數(shù)據(jù)類型: 是否相等?

引用數(shù)據(jù)類型: 是否指向堆內存的同一地址?

class User{

String name;

int age;

}

class Test{

public static void main(String[] args){

User u1 = new User();

User u2 = new User();

User u3 = u1;

boolean b1 = u1 == u2; // false

boolean b2 = u1 == u3; // true

}

}

eqauls的復寫

兩個對象類型相同(使用instanceof來比較)

兩個對象的成員變量的值完全相同

class User{

// String是引用數(shù)據(jù)類型

String name;

int age;

public User(String name, int age){

this.name = name;

this.age = age;

}

public boolean equals(Object obj){

if(this == obj){

return true;

}

if(obj instanceof User){

User u = (User)obj;

// 這里的equals是Object的

// equals用于比較內容是否相等

if (this.age == u.age && this.name.equals(u.name)){

return true;

}

else{

return false;

}

}

else{

return false;

}

}

}

class Test{

public static void main(String[] args){

User u1 = new User("zahng",12);

User u2 = new User("liu",15);

User u3 = new User("zahng",12);

boolean b1 = u1.equals(u2); // false

boolean b2 = u1.equals(u3); // true

}

}

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的java高级教程_Java高级教程02的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。