[TOC]
1.Java线程
1.1. 多线程和多进程
- 多进程:操作系统能够同时进行多个任务: 每个app(word,播放器,浏览器)可以同时运行
- 多线程:同一应用程序中哟多个顺序流同时执行
- 线程是进程中的一部分
1.2. 线程的执行过程:

主要过程:

<font color = "red">多线程执行的抢CPU是不规律的,由虚拟机分配</font>
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);
1t1.setName("thread a"); 2t2.setName("thread b"); 3// t1先获得锁,运行,t2等待 4// t2然后获得锁,运行,t1等待 5t1.start(); 6t2.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();
1Thread t1 = new Thread(new MyThread1(service)); 2Thread t2 = new Thread(new MyThread2(service));} }
2.3. 同步方法
-
同步方法锁住的是this
class Service { // 同步方法只需要在方法名前加入synchronized即可 public synchronized void fun1(){
1 try{ 2 Thread.sleep(3*1000) 3 } 4 catch(Exception e){ 5 System.out.println(e) 6 } 7 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];1// 打印二位数组 2for (int i=0; i<arr.length;i++){ 3 for (int j=0; i<arr[i].length; i++){ 4 System.out.println(arr[i][j]); 5 } 6}} }
3.2. Java的类集框架
1. 类集框架的定义和种类,以及基础结构
- 类集框架是一组类和结构,位于java.util包中,主要用于储存和管理对象,主要分为三大类:
- 集合(Set): 对象不按照特定的方式排序,并且没有重复对象
- 列表(List): 对象按照索引位置排序,可以有重复对象
- 映射(dictionary): 通过键-值对储存(key-value)
类集框架的主体结构

2. ArrayList列表的使用
1import java.util.List; 2import java.util.ArrayList; 3 4 5public class Test{ 6 public static void main(String[] args){ 7 // arraylist的长度可以自动扩展,跟数组有区别 8 // 声明arraylist只能存String类型 9 ArrayList<String> arraylist = new ArrayList<String>(); 10 // 向arraylist数组添加对象 11 arraylist.add("a"); 12 arraylist.add("b"); 13 // 从arraylist取对象 14 String s = arraylist.get(1); 15 // 打印arraylist数据 16 for(int i=0; i<arraylist.size(); i++){ 17 String s = arraylist.get(1); 18 System.out.println(s); 19 } 20 // 删除arraylist数据 21 arraylist(1); 22 } 23} 24
3. Collection和Iterator接口
- Iterator最高层---
hasNext()+Next() - Collection接口是Iteator的子接口
- Set是Collection接口的子接口
- HashSet是Set的实现类
Iterator <-- Collection <-- Set <-- HashSetIterator <-- Collection <-- List <-- ArrayList
(1) Collection接口
方法
含义
boolean add(Object 0)
向集合添加对象
void clear()
删除集合的所有对象
boolean isEmpty()
判断集合是否为空
remove(Object o)
从集合中删除一个对象的引用
int size()
返回集合中元素的数组
4.Set和HashSet用法(Collection的实现类)
1import java.util.Set; 2import java.util.HashSet; 3 4public class Test{ 5 public static void main(String[] args){ 6 HashSet<String> hashset = new HashSet<String>(); 7 Set<String> set = hashset; 8 9 boolean b1 = set.isEmpty(); 10 11 set.add("a"); 12 set.add("b"); 13 set.add("c"); 14 set.add("a"); // 重复元素会忽略 15 16 int a = set.size(); 17 set.remove(a); 18 set.clear(); 19 20 // 集合取数据---通过迭代器Iterator 21 // 调用Set对象的Iterator方法,会生成一个迭代器对象,该对象用于遍历整个Set 22 Iterator<String> it = set.iterator(); 23 24 while(it.hasNext()){ 25 //取出元素,并将指针向后面挪一位 26 String s = it.next(); 27 System.out.println(s); 28 } 29 30 } 31}
5. Map和HashMap的使用方法
-
Map <-- HashMapimport 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;
1map.put("1","a"); 2map.put("2","b"); 3map.put("3","c"); 4map.put("3","e"); // 将会覆盖上面的键值对 5 6int i = map.size(); 7 8String s = map.get("3"); 9} }
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;
1boolean b1 = u1 == u2; // false 2boolean 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);
1boolean b1 = u1.equals(u2); // false 2boolean b2 = u1.equals(u3); // true} }