Java基礎-多線程以及Collection+Map集合接口

阿里雲大學課27. Thread類實現多線程

要想實現多線程必須有一個多線程的執行主類。
主類的定義結構:java

  • 繼承一個Thread類
  • 【推薦-避免單繼侷限】實現Runnable、Callable接口

java.lang.Thread 直接繼承Thread類並覆寫類中的run()方法(至關於線程的主方法)算法

class MyThread extends Thread {
    //線程主體類
    private String title;
    public MyThread(String title) {
        this.title = title;
    }
    @Override
    public void run() { // 全部的線程今後處開始執行
        for (int x = 0; x < 10; x++) {
            System.out.println(this.title + ", x = " + x);
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        MyThread mt1 = new MyThread("線程A");
        MyThread mt2 = new MyThread("線程B");
        MyThread mt3 = new MyThread("線程C");
        mt1.start(); //錯誤的啓動: mt1.run();
        mt2.start();jichu
        mt3.start(); 
    }
}

錯誤的啓動多線程-程序執行結果

只是順序打印
正確啓動多線程的方式應該調用的是Thread類當中的start()方法
多線程啓動只有一個方法:public void start()

數組

多線程啓動示意圖

課28. Runnable 接口實現多線程

Runnable裏只有一個run()方法, 和Thread相同,安全

@FunctionalInterface
public interface Runnable {
    public void run();
}

//利用Runnable 定義線程主體類
class MyThread implements Runnable {
    //線程主體類
    private String title;
    public MyThread(String title) {
        this.title = title;
    }
    @Override
    public void run() { // 全部的線程今後處開始執行
        for (int x = 0; x < 10; x++) {
            System.out.println(this.title + ", x = " + x);
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        MyThread mt1 = new MyThread("線程A");
        MyThread mt2 = new MyThread("線程B");
        MyThread mt3 = new MyThread("線程C");
        new Thread(mt1).start(); //匿名內部類傳入一個接口
        new Thread(mt2).start();
        new Thread(mt3).start();
    }
}

Thread與Runnable的區別
public class Thread extends Object implement Runnable
實現Runnable方法會致使無法調用Thread類中的start()方法,稍微拐個彎用Thread的構造方法public Thread(Runnable target)來接收Runnable接口對象,而後再直接調用start()方法多線程

課29 Thread與Runnable的區別

Runnable避免了單繼承侷限,
Thread類的繼承定義形式
public class Thread extends Object implements Runnable
Thread是Runnable接口的子類併發

@Override
public run() {
    if (target != null) {

線程池:多個線層封裝在一塊兒進行操做
爲何須要線程池?
例子:兄弟們,有個活,3天完成,20我的一塊兒幹 20我的就是一個線程池異步

java.util.concurrent從JDK 1.5以後的添加的併發數據包ide

Interface ExecutorService
Interface ScheduledExecutorService
若是要進行線程池的建立性能

建立無限大小 線程對象
建立固定大小的線程池
建立定時調度池
```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;學習

public class Demo {
public static void main(String[] args) throws Exception {
//建立一個線程池的模型
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
for (int x = 0; x < 10; x++) {
executorService.submit(() -> {
System.out.println(Thread.currentThread().getName() + "、 x = " + index);
});
}
executorService.shutdown();

使用Object類當中的喚醒與等待機制進行操做
public final void notify() //喚醒第一個等待線程

public final void notifyAll() //

class Data {
    private String title;
    private note;

    private boolean flag = true;

    public synchronized void set(String title, String note) {
        if (this.flag = false) {
            try {
                super.

請解釋sleep() 與 wait()的區別
sleep() 是Thread類中定義的方法,到了必定的時間後該休眠的線程能夠自動喚醒
wait() 是Object類中定義的方法,若是想要喚醒,必須使用notify()、notifyAll() 才能喚醒

Java的類集之中(java.util包) 當中提供的兩個最爲重要核心的操做接口: Collection + Map

Collection 與鏈表相似:每一次進行數據操做的時候只可以對單個的對象進行處理,因此Collection是單個集合保存的最大父接口

Collection接口的定義

public interface <E> extends Iterable<E>

最重要的幾個方法:
子接口都有的兩個方法
* public boolean add(E)
* public Iterator iterator 取得Iterator

考慮Collection接口的子接口: List(容許重複) Set(不容許重複)

Collection接口定義

List接口

兩個重要的擴充方法:
* public E get(int index) //根據索引取得數據
* public E set(int index, E element) //修改數據
List使用比例能夠達到Collection的 80%
線程的同步和死鎖,概念很重要

List三個經常使用子類:ArrayList, Vector, LinkedList

全部子類的方法均可以參考接口中的定義,使用形式上相同

對比區別:
ArrayList: 一個針對於List接口的數組操做實現
每個線程對象操做的延遲問題(輪番搶佔資源致使)
先作一個簡單的程序:實現多個線程賣票的處理

//source: aliyun java學習路線圖-高級【課時35】
class MyThread implements Runnable {
    private int ticket = 10; //要賣出的總票數
    
    @Override
    public void run() {
        for (int x = 0; x < 20; x++) {
            if (this.ticket > 0) {
                try
                System.out.println(Thread.currentThread().getName() + "賣票, ticket = " + this.ticket;
            }
        }
    }

    public class Demo {
        public static void main(String[] args) throws Exception {
            System.out.println(Thread.currentThread().getPriority());
        }
    }
}

Set接口集合

TreeSet排序分析

使用TreeSet排序,須要Comparable類的支持,全部屬性都須要參與比較

import java.util.LinkedList;
class Person {
    private String

List
AbstractSet -中間的抽象類
HashSet:無序存儲
TreeSet: 有序存儲

Hash算法

如何解決
1.同步代碼塊:

  • 在方法裏面進行攔截,用同步代碼塊必需要設置一個鎖定的對象
  1. HashTable

HashMap
請解釋HashMap的原理

請解釋HashMap與HashTable的區別

Concurrent HashMap

區別 HashMap HashTable
推出版本 JDK 1.2推出 JDK 1.0推出
性能 異步處理、性能高 同步處理、性能較低
安全性 非線程安全 線程安全
null操做 容許存放null key 和 value都不容許爲空,不然會出現NullPointerException

多考慮HashMap

不是傳統的軟件開發而是互聯網的項目了

相關文章
相關標籤/搜索