基本概念
程序
一个静态概念,一般对应于操作系统中的一个可执行文件,比如点击可执行程序,加载程序到内存中,开始执行程序,产生了“进程”
进程
进程(process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是结构的基础。
- 每个进程由三部分组成cpu、data、code, 每个进程都是独立的,保有自己的cpu时间、代码和数据
windows系统通过任务管理器查看进程,linux系统ps 或 top
线程
一个进程产生多个线程。同一进程的多个线程可以共享此进程的某些资源(代码、数据),线程又被称作轻量级进程(lightweight process)
green threads是一种由运行环境或者虚拟机(VM)调度,而不是由本地底层操作系统调度的线程。绿色线程并不依赖底层的系统功能,模拟实现了多线程的运行,这种线程的管理调配发生在用户空间而不是内核空间,所以他们可以在没有原声线程支持的环境中工作。
- java1.1中绿色线程是jvm中唯一一种线程模型
- java1.2中,linux中的jvm是基于pthread实现的,即java中线程的本质就是操作系统中的线程
Thread类中的start(),调用的就是native方法start0(),即操作系统底层方法
如何实现多线程
继承Thread类实现多线程
在java中负责实现线程功能的类是java.lang.Thread类
- 通过创建Thread的实例来创建新的线程
- 每个线程都是通过某个特定的Thread对象所对应的run()方法来完成其操作的,方法run()称为线程体
调用Thread类的start()方法来启动一个线程,启动后运行run方法中的代码
public class TestThread extends Thread {// 重写run方法。run是方法体@Overridepublic void run() {for(int i=0; i<10; i++){System.out.println(this.getName() + ":" + i); // getName线程名称}}public static void main(String[] args){TestThead t1 = new TestThread();t1.start();TestThead t2 = new TestThread();t2.start();}}// 启动后可以发现两个线程切换执行
使用Runnable接口
```java public class TestThread2 implements Runnable{ @Override public void run() {
for(int i=0; i<10; i++){System.out.println(Thread.currentThread().getName() + ":" + i);}
}
public static void main(String[] args){
// 把实现Runnable接口的对象作为参数传入TestThead2 t2 = new TestThread2();Thread t1 = new Thread(t);t1.start();TestThead t2 = new Thread(t);t2.start();
}
}
<a name="Tf3FL"></a>## 使用lambda语法(JDK8新增)- 接口中只有一个方法时,可以使用lambda语法```javapublic class TestThread3 {public static void main(String[] args){// 匿名内部类new Thread(new Runnable() {@Overridefor(int i=0; i<10; i++){System.out.println(Thread.currentThread().getName() + ":" + i);}}).start();// 接口中只有一个方法时,可以使用lambda语法new Thread(()->{for(int i=0; i<10; i++){System.out.println(Thread.currentThread().getName() + ":" + i);}}).start();}}
线程的状态和生命周期
终止线程的典型方式
package com.bob;public class TestThreadTerminated implements Runnable{private boolean live = true;@Overridepublic void run() {int i = 0;while (live) {System.out.println(Thread.currentThread().getName() + ":" + (i++));}}public void terminate() {live = false;}public static void main(String[] args) {TestThreadTerminated tt1 = new TestThreadTerminated();Thread t1 = new Thread(tt1); // 新生状态t1.start(); // 就绪状态for (int i = 0; i < 100; i++) {System.out.println("main 方法主线程" + i);}tt1.terminate();System.out.println("主线程调用tt1 方法,终止子线程");}}
暂停线程执行
- sleep(进入阻塞状态):Thread.sleep(1000),时间到后进入runnable状态
yield (回到就绪状态,让出让别的线程先执行):Thread.yield(),主动失去执行权,回到runnable状态,当然不主动调用yield的线程也有可能回到runnable状态
线程的联合join方法
联合线程执行完毕,原线程继续执行 ```java class FatherThread implements Runnable { public static void main {
Thread farther = new Thread(new FatherThread());farther.start()
} public void run() {
Thread son = new Thread(new SonThread());son.start();try {son.join()}catch{...// 结束jvm,0是正常结束、1是非正常结束System.exit(1)}
} }
class SonThread implements Runnable { public void run() { // …等待执行的逻辑 } }
<a name="pugMP"></a>## 获取线程基本信息- 线程的常用方法| **方法** | **功能** || --- | --- || **isAlive()** | **判断线程是否还活着,即线程是否还未终止** || **getPriority()** | **获得线程的优先级数值** || **setPriority()** | **设置线程的优先级数值** || **setName()** | **给线程一个名字** || **getName()** | **取得线程的名字** || **currentThread()** | **取得正在运行的线程对象,也就是取得自己本身** |<a name="amwWw"></a>## 线程的优先级- 处于就绪状态的线程,会进入“就绪队列”等待JVM来挑选- 线程的优先级用数字表示,范围从**1到10**,一个线程的缺省优先级是**5**- 优先级低只是意味着获得调度的**概率**低,并不是绝对优先调用优先级高的线程,后调用优先级低的线程```javaThread t1 = new Thread(tt,"一个优先级很高的线程");t1.setPriority(10)
线程同步
- 同步问题发生于同一个资源,多人都想使用的情况,这时就会发生等待机制
- 多个线程访问同一个对象,并且某些线程需要修改对象,这是就需要线程同步
- 线程同步是一种等待机制,多个需要同时访问此对象的线程进入这个对象的等待池形成队列,等待前面的线程使用完毕后,下一个线程再使用
- 通过synchronized关键字实现线程同步:包括synchronized方法和synchronized块
- synchronized方法控制对“对象的成员变量”的访问,每个对象对应一把锁,每个synchronized方法都必须获得调用该方法的对象的锁才能执行,否则所属线程阻塞,方法一旦执行,就独占该锁,指导从方法返回时才将锁释放,此后被阻塞的线程方能获得该锁,重新进入可执行状态
- synchronized方法有缺陷,若讲一个很长的方法体设置为synchronized会大大降低效率,java提供了synchronized块以精确的控制到具体的成员变量,缩小同步范围,提高效率,通过synchronized关键字来声明synchronized块
// synchronized方法public synchronized void accessVal(int newVal);// synchronized块synchronized(syncObject){// 允许访问的代码}
无同步引入的安全性问题
```java package no;
public class TestSynchronized { public static void main(String[] args) { Account a1 = new Account(100,”张三”); Drawing d1 = new Drawing(80,a1); Drawing d2 = new Drawing(60,a1); Thread t1 = new Thread(d1); Thread t2 = new Thread(d2); t1.start(); t2.start(); } }
class Account { int money; String name;
public Account(int money, String name) {this.money = money;this.name = name;}
}
class Drawing implements Runnable{ int drawingNum; // 取出了多少钱 Account account; int expenseTotal; // 总共取了多少钱
public Drawing(int drawingNum, Account account) {this.drawingNum = drawingNum;this.account = account;}@Overridepublic void run() {if (account.money< drawingNum){System.out.println("没钱");return;}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}account.money -= drawingNum;expenseTotal += drawingNum;System.out.println(Thread.currentThread().getName() + "账户余额" + account.money);System.out.println(Thread.currentThread().getName() + "总共取了" + expenseTotal);}
}
<a name="QSkzz"></a>## 使用Synchronized解决安全性问题```java// 在run方法加入synchronieze块 解决问题@Overridepublic void run() {synchronized (account) {if (account.money< drawingNum){System.out.println("没钱");return;}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}account.money -= drawingNum;expenseTotal += drawingNum;}System.out.println(Thread.currentThread().getName() + "账户余额" + account.money);System.out.println(Thread.currentThread().getName() + "总共取了" + expenseTotal);}
生产者消费者模式
生产者消费者概念
- 多线程环境下,经常需要多个线程的并发和协作。生产者/消费者模式是一个重要的多线程并发协作模型
- 生产者指的是负责生产数据的模块(方法、对象、线程、进程)
- 消费者指的是负责处理数据的模块(方法、对象、线程、进程)
- 缓冲区:消费者不能直接使用生产者的数据,两者之间存在缓冲区。生产者将生产好的数据放入缓冲区,消费者从该缓冲区中取需要处理的数据
- 线程并发协作(线程通信),通常用于生产者/消费者模式。需要使用wait()/notify()/notifyAll()方法
- 线程通信常用方法均是java.lang.Object 类的方法
- 通信方法只能在synchronized方法或块中使用,否则异常
- 线程执行wait()方法的时候,会释放当前的锁,让出cpu,进入等待状态 | 线程通信方法名 | 功能 | | —- | —- | | final void wait() | 表示线程释放共享资源锁,进入等待队列 | | void wait(long timeout) | 线程等待指定毫秒数的时间 | | final void notify() | 随机唤醒一个等待同一共享资源的线程 | | final void notifyAll() | 唤醒同一个对象上所有调用wait()方法的线程 |
代码示例
package bob;public class PCmodel {public static void main(String[] args) {bufferZone appleBasket = new bufferZone();Producer p1 = new Producer(appleBasket);Consumer c1 = new Consumer(appleBasket);p1.start();c1.start();}}class apple {int id;public apple(int id) {this.id = id;}}class bufferZone {int index = 0;apple[] as = new apple[10];public synchronized void push(apple a) {while (as.length == index) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}this.notify();as[index] = a;index ++;System.out.println(Thread.currentThread().getName() + "生产一个苹果" + a.id);}public synchronized apple pop() {while (index == 0) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}this.notify();index --;apple a = as[index];System.out.println(Thread.currentThread().getName() + "消费一个苹果"+ a.id);return a;}}class Producer extends Thread{bufferZone bz;public Producer(bufferZone bz) {this.bz = bz;}@Overridepublic void run() {for (int i = 0; i < 20; i++) {apple a = new apple(i);bz.push(a);}}}class Consumer extends Thread{bufferZone bz;public Consumer(bufferZone bz) {this.bz = bz;}@Overridepublic void run() {for (int i = 0; i < 20; i++) {apple a = bz.pop();}}}
