Fork me on GitHub

Java高级教程02

1.Java线程

1.1. 多线程和多进程

  • 多进程:操作系统能够同时进行多个任务: 每个app(word,播放器,浏览器)可以同时运行
  • 多线程:同一应用程序中哟多个顺序流同时执行
  • 线程是进程中的一部分

1.2. 线程的执行过程:

主要过程:

多线程执行的抢CPU是不规律的,由虚拟机分配

1.3. 创建线程的方法

(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状态---->准备抢CPU
    fo.start();
    ft.start();
  }
}

(2). 方法2: 复写Runnable接口(推荐)

  • 提供一个实现接口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接口实现类的对象
    RunnableImpl ri = new RunnableImpl();
    //生成一个Thread对象,并将Runnable接口实现类的对象作为参数传递给该Thread对象
    Thread t = new Thread(ri);
    // 通知thread执行
    t.start();
  }
}

1.4. 线程的简单控制

  • 中断线程
    • Thread.sleep():先睡眠,然后继续进入就绪状态,准备抢CPU----记得抛出异常哦,亲
    • Thread.yield():让出CPU,然后继续进入就绪状态,准备抢CPU
  • 设置线程的优先级:
    • getPriority(): 获取优先级
    • setPriority(): 设置优先级(1-10)

2. Java线程同步synchronized

2.1. 多线程数据安全以及synchronized的使用

  • 多线程共用同一份数据的时候,会出问题
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, 只要一个对象获得同步锁,这个对象其他也含有同步锁的代码都不能执行,只能释放后才能执行
  • 没有同步锁的代码块跟同步锁无关,会继续执行,没有影响
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的数组和类集框架

  • 用于储存一些列相同数据类型的容器

3.1. 数组类型

  • 数组长度一旦声明,不可更改
class Test{
  public static void main(String[] args){
    // 一维数组的静态声明法
    int[] arr = {1,2,5,7,8,10};
    arr[3] = 10; // 设置数组元素为新的值
    // 打印一维数组元素
    for (int  i=0; i<arr.length; i++){
      System.out.println(arr[i]);
    }
    // 一维数组的动态声明法  
    int[] arr = new int[10];  // 初始化为0
    // 二位数组的静态声明法
    int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
    arr[1][1];  // = 5
    // 二位数组的动态声明法
    int[][] arr = new int[3][5];
    
    // 打印二位数组
    for (int i=0; i<arr.length;i++){
      for (int j=0; i<arr[i].length; 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的长度可以自动扩展,跟数组有区别
    // 声明arraylist只能存String类型
    ArrayList<String> arraylist = new ArrayList<String>();
    // 向arraylist数组添加对象
    arraylist.add("a");
    arraylist.add("b");
    // 从arraylist取对象
    String s = arraylist.get(1);
    // 打印arraylist数据
    for(int i=0; i<arraylist.size(); i++){
      String s = arraylist.get(1);
      System.out.println(s);
    }
    // 删除arraylist数据 
    arraylist(1);
  }
}

3. Collection和Iterator接口

  • Iterator最高层---hasNext() + Next()
  • Collection接口是Iteator的子接口
  • Set是Collection接口的子接口
  • HashSet是Set的实现类
  • Iterator <-- Collection <-- Set <-- HashSet
  • Iterator <-- Collection <-- List <-- ArrayList

(1) Collection接口

方法 含义
boolean add(Object 0) 向集合添加对象
void clear() 删除集合的所有对象
boolean isEmpty() 判断集合是否为空
remove(Object o) 从集合中删除一个对象的引用
int size() 返回集合中元素的数组

4.Set和HashSet用法(Collection的实现类)

import java.util.Set;
import java.util.HashSet;

public class Test{
  public static void main(String[] args){
    HashSet<String> hashset = new HashSet<String>();
    Set<String> 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();

    // 集合取数据---通过迭代器Iterator
    // 调用Set对象的Iterator方法,会生成一个迭代器对象,该对象用于遍历整个Set
    Iterator<String> it = set.iterator();

    while(it.hasNext()){
      //取出元素,并将指针向后面挪一位
      String s = it.next();
      System.out.println(s);
    }

  }
}

5. Map和HashMap的使用方法

  • Map <-- HashMap
import java.util.Map;
import java.util.HashMap;

public class Test{
  public static void main(String[] args){
    // 创建hashmap对象,并定义键值对类型
    HashMap<String, String> hasMap = new HashMap<String, String>();
    Map<String, String> 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函数的使用方法

4.1. equals的作用

==的作用:

  • 基本数据类型: 是否相等?
  • 引用数据类型: 是否指向堆内存的同一地址?
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是引用数据类型
  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
  }
}
posted @ 2019-01-16 15:35  Bricker666  阅读(959)  评论(0编辑  收藏  举报