多线程技术概述
线程与进程
进程:是指一个内存中运行的应用程序,每个进程都有一个独立的内存空间
线程:①是进程中的一个执行路径,共享一个内存空间,线程之间可以自由切换,并发执行. 一个进程最少
有一个线程
②线程实际上是在进程基础之上的进一步划分,一个进程启动之后,里面的若干执行路径又可以划分
成若干个线程
线程调度
分时调度
所有线程轮流使用 CPU 的使用权,平均分配每个线程占用 CPU 的时间。
抢占式调度
优先让优先级高的线程使用 CPU,如果线程的优先级相同,那么会随机选择一个(线程随机性),Java使用的为抢占式调度。
CPU使用抢占式调度模式在多个线程间进行着高速的切换。对于CPU的一个核新而言,某个时刻,只能执行一个线程,而 CPU的在多个线程间切换速度相对我们的感觉要快,看上去就是 在同一时刻运行。 其实,多线程程序并不能提高程序的运行速度,但能够提高程序运行效率,让CPU的 使
用率更高。
同步与异步
同步:排队执行,效率低但是安全。
异步:并行执行,效率高但是不安全。
并发与并行
并发:指两个或多个事件在同一个时间段内发生。
并行:指两个或多个事件在同一刻发生(同时发生)。
多线程技术
实现Runnable 与 继承Thread相比有如下优势:
* 1. 通过创建任务,然后给程序分配的方式来实现的多线程,更适合多个线程同时执行相同的任务的情况
* 2. 可以避免单线程所带来的的局限性
* 3. 任务与线程本身是分离的,提高了程序的健壮性
* 4. 后续的线程池技术,接收Runnable类型的任务,不接收Thread类
Thread继承
**实现Thread**
ThreadTest test=new ThreadTest();
test.start();
for (int i = 1; i <100; i++) {
System.out.println("看电视");
}
实现runnable
1.
//1. 创建一个任务对象
MyRunnable mr = new MyRunnable();
//2. 创建一个线程并为其分配一个任务
Thread t = new Thread(mr);
//执行这个线程
t.start();
for (int i = 1; i <100; i++) {
System.out.println("看电视ing");
}
public class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("吃饭ing");
}
}
}
设置和获取线程名称
public class ThreadSetGet {
public static void main(String[] args) {
//获取当前运行的线程的对象名称
System.out.println(Thread.currentThread().getName());
new Thread(new MyTest(),"李狗蛋1号").start();
new Thread(new MyTest(),"李狗蛋2号").start();
new Thread(new MyTest(),"李狗蛋3号").start();
}
static class MyTest implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
}
1.线程休眠sleep
for (int i = 0; i < 10; i++) {
System.out.println(i);
//每次循环休眠1秒种 以毫秒为单位
Thread.sleep(1000);
}
2.线程阻塞
堵在那里等待运行完成
3.线程中断
打入一个中断标记
4.守护线程
守护用户线程,当最后一个用户线程结束时,所有守护线程自动死亡。
- 线程:分为守护线程和用户线程
- 用户线程:当一个进程不包含任何的存活的用户线程时,运行结束。
Thread t1=new Thread();
//设置为守护线程
t1.setDaemon(true);
t1.start();
for (int i = 0; i < 5; i++) {
System.out.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//终止代码
e.printStackTrace();
}
}
//给线程t1添加中断标记
t1.interrupt();
}
static class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//e.printStackTrace();
System.out.println("发现中断标记,自杀死亡");
//返回终止
return;
}
}
}
线程安全问题
public class SaftyProblem {
public static void main(String[] args) {
Runnable runnable=new Ticket();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
static class Ticket implements Runnable {
//票数
private int count = 10;
@Override
public void run() {
//卖票
while (count>0){
System.out.println("正在准备卖票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println("出票成功,余票"+count);
}
}
//最后导致三个线程同时处在循环中卖最后一张票时导致出现负票数
}
}
解决方案1. 同步代码块
格式:synchronized (锁对象){}
public class Demo001 {
public static void main(String[] args) {
Runnable runnable = new Ticket();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
static class Ticket implements Runnable {
//票数
private int count = 10;
private Object o = new Object();
@Override
public void run() {
while (true) {
synchronized (o) {
if (count > 0) {
System.out.println("正在准备卖票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println(Thread.currentThread().getName() + "出票成功,余票" + count);
} else {
break;
}
}
}
}
}
}
解决方案2. 同步方法
public class Demo002 {
public static void main(String[] args) {
/*
* 解决方案2. 同步方法
* */
Runnable runnable = new Ticket();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
static class Ticket implements Runnable {
//票数
private int count = 10;
@Override
public void run() {
//卖票
while (true) {
boolean flag = sale();
if (!flag) {
break;
}
}
}
private synchronized boolean sale() {
while (count > 0) {
System.out.println("正在准备卖票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println(Thread.currentThread().getName()+"出票成功,余票" + count);
return true;
}
return false;
}
}
}
解决方案3. 显示锁Lock 子类 ReentrantLock
public class Demo003 {
public static void main(String[] args) {
/*
* 解决方案2. 同步方法
* */
Runnable runnable = new Ticket();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
static class Ticket implements Runnable {
//票数
private int count = 10;
//显示锁 fair的参数为true就是公平锁:谁先来谁先的到这个锁
private Lock l=new ReentrantLock();
@Override
public void run() {
//卖票
while (true) {
l.lock();
if (count>0) {
System.out.println("正在准备卖票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println(Thread.currentThread().getName()+"出票成功,余票" + count);
}else {
break;
}
l.unlock();
}
}
}
}
线程死锁
A等B B等A
例子:罪犯与警察↓↓↓
public class Demo004 {
/*
* 线程死锁 A等B B等A
* */
public static void main(String[] args) {
Culprit c = new Culprit();
Police p = new Police();
new MyThread(c,p).start();
c.say(p);
}
static class MyThread extends Thread{
private Culprit c;
private Police p;
public MyThread(Culprit c, Police p) {
this.c=c;
this.p=p;
}
@Override
public void run() {
p.say(c);
}
}
static class Culprit {
public synchronized void say(Police p) {
System.out.println("警察,你放了我,我就放人质");
p.fun();
}
public synchronized void fun() {
System.out.println("罪犯被放走了,罪犯也放了人质");
}
}
static class Police {
public synchronized void say(Culprit c) {
System.out.println("罪犯,你放了人质,我就放了你");
c.fun();
}
public synchronized void fun() {
System.out.println("警察放了罪犯,并且罪犯跑了");
}
}
}
多线程通信问题
例子: 生产者与消费者问题↓↓↓
public class Demo5 {
public static void main(String[] args) {
Food f = new Food();
new Cook(f).start();
new Waiter(f).start();
}
//厨师
static class Cook extends Thread {
private Food f;
public Cook(Food f) {
this.f = f;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
f.SetNameAndtaste("老干妈小米粥", "香辣味");
} else {
f.SetNameAndtaste("煎饼果子", "甜辣味");
}
}
}
}
//服务生
static class Waiter extends Thread {
private Food f;
public Waiter(Food f) {
this.f = f;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
f.get();
}
}
}
//食物
static class Food{
private String name;
private String taste;
//true表示可以生产
private Boolean flag = true;
public synchronized void SetNameAndtaste(String name, String taste) {
if (flag) {
this.name = name;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.taste = taste;
flag = false;
this.notifyAll();
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//端菜
public synchronized void get() {
if (!flag) {
System.out.println("服务员端走的菜的名称:" + name + "味道:" + taste);
flag = true;
this.notifyAll();
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}