进程是程序运行的实例;
当一个程序执行进入内存运行时,即变成一个进程;
进程的资源是彼此隔离的,其他进程不允许访问
线程是进程内执行的“任务”;
线程是进程内的一个“基本任务”,每个线程都有自己的功能,是CPU分配与调度的基本单位;
一个进程内可以包含多个线程,反之一个线程只能隶属于某一个线程;
进程内至少拥有一个“线程”,这个线程叫“主线程”,主线程消亡则进程结束;
单核CPU通过时间片使所有线程达到一个并发执行的效果;
多核CPU达到并行执行的效果;
package org.example;
import java.util.Random;
public class ThreadExample1 {
class Runner extends Thread {
@Override
public void run() {
Integer speed = new Random().nextInt(10);
for (int i = 1; i <= 10; i++) {
System.out.println("第" + i + "秒:" + this.getName() + "跑到:" + (i * speed));
}
}
}
public void start() {
Runner runnerA = new Runner();
runnerA.setName("参赛者A");
Runner runnerB = new Runner();
runnerB.setName("参赛者B");
runnerA.start();
runnerB.start();
}
// 主线程存在,进程存在,主线程消失,进程消失
public static void main(String[] args) {
new ThreadExample1().start();
}
}
当调用thread.start()方法时,线程对象A和B中的run方法执行权移交给了操作系统,由操作系统的线程调度策略来决定先执行哪个方法
package org.example;
import java.util.Random;
public class ThreadExample2 {
class Runner implements Runnable {
@Override
public void run() {
Integer speed = new Random().nextInt(10);
for (int i = 1; i <= 10; i++) {
System.out.println("第" + i + "秒:" + Thread.currentThread().getName() + "跑到:" + (i * speed));
}
}
}
public void start() {
Runner runnerA = new Runner();
Thread threadA = new Thread(runnerA);
threadA.setName("参赛者A");
Runner runnerB = new Runner();
Thread threadB = new Thread(runnerB);
threadB.setName("参赛者B");
threadA.start();
threadB.start();
}
// 主线程存在,进程存在,主线程消失,进程消失
public static void main(String[] args) {
new ThreadExample2().start();
}
}
package org.example;
import java.util.Random;
import java.util.concurrent.*;
public class ThreadExample3 {
// callable线程执行后将结果返回
class Runner implements Callable<Integer> {
public String name;
@Override
public Integer call() throws Exception {
Integer speed = new Random().nextInt(10);
Integer result = 0;
for (int i = 1; i <= 10; i++) {
System.out.println("第" + i + "秒:" + this.name + "跑到:" + (i * speed));
result += i * speed;
}
return result;
}
}
public void start() throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(3);
Runner threadA = new Runner();
threadA.name = "参赛者A";
Runner threadB = new Runner();
threadB.name = "参赛者B";
Runner threadC = new Runner();
threadC.name = "参赛者C";
Future<Integer> submit1 = executorService.submit(threadA);
Future<Integer> submit2 = executorService.submit(threadB);
Future<Integer> submit3 = executorService.submit(threadC);
executorService.shutdown();
System.out.println(threadA.name + "累计跑了" + submit1.get() + "米");
System.out.println(threadB.name + "累计跑了" + submit2.get() + "米");
System.out.println(threadC.name + "累计跑了" + submit3.get() + "米");
}
// 主线程存在,进程存在,主线程消失,进程消失
public static void main(String[] args) throws ExecutionException, InterruptedException {
new ThreadExample3().start();
}
}
Thread类定义的线程六种状态:
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
*
* - {@link Object#wait() Object.wait} with no timeout
* - {@link #join() Thread.join} with no timeout
* - {@link LockSupport#park() LockSupport.park}
*
*
* A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called {@code Object.wait()}
* on an object is waiting for another thread to call
* {@code Object.notify()} or {@code Object.notifyAll()} on
* that object. A thread that has called {@code Thread.join()}
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
*
* - {@link #sleep Thread.sleep}
* - {@link Object#wait(long) Object.wait} with timeout
* - {@link #join(long) Thread.join} with timeout
* - {@link LockSupport#parkNanos LockSupport.parkNanos}
* - {@link LockSupport#parkUntil LockSupport.parkUntil}
*
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
代码中的同步机制:
package org.example;
public class SyncSample {
class Printer {
Object lock = new Object();
public void print() throws InterruptedException {
// 代码块
synchronized (lock) {
Thread.sleep(1000);
System.out.println("你");
Thread.sleep(1000);
System.out.println("好");
Thread.sleep(1000);
System.out.println("北");
Thread.sleep(1000);
System.out.println("京");
}
}
// 方法【实际使用更广泛】
public synchronized void print2() throws InterruptedException {
// 代码块
Thread.sleep(1000);
System.out.println("你");
Thread.sleep(1000);
System.out.println("好");
Thread.sleep(1000);
System.out.println("北");
Thread.sleep(1000);
System.out.println("京");
}
// 方法【实际使用更广泛】
public synchronized void print3() throws InterruptedException {
// 代码块
Thread.sleep(1000);
System.out.println("你");
Thread.sleep(1000);
System.out.println("好");
Thread.sleep(1000);
System.out.println("北");
Thread.sleep(1000);
System.out.println("京");
}
}
class PrintTask implements Runnable {
private Printer printer;
@Override
public void run() {
try {
printer.print2();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void start() {
Printer printer = new Printer();
for(int i = 0; i < 10; i++) {
PrintTask printTask = new PrintTask();
printTask.printer = printer;
Thread thread = new Thread(printTask);
thread.start();
}
}
public static void main(String[] args) {
new SyncSample().start();
}
}
死锁是在多线程情况下最严重的问题,在多线程对公共资源(文件、数据)等进行操作时,彼此不释放自己的资源,而去试图操作其他线程的资源,而形成交叉引用,就会产生死锁。
解决死锁最根本的建议是:
并发是伴随着多核处理器的诞生而产生的,为了充分利用硬件资源,诞生了多线程技术。但是多线程又存在资源竞争的问题,引发了同步和互斥的问题,JDK1.5推出的java.util.concurrent(并发工具包)来解决这些问题。
在Java.util.concurrent中,提供了工具类Executors(调度器)对象来创建线程池,可创建的线程池有四种:
1、FixedThreadPool:定长线程池
public static void main(String[] args) {
// 创建一个定长线程池
// 定长线程池的特点是固定线程总数,空闲线程用于执行任务,如果线程都在使用,后续任务则处理等待状态
ExecutorService threadPool = Executors.newFixedThreadPool(10);
for(int i = 0; i < 100; i++) {
// 执行runnable对象
final int index = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "---" + index);
}
});
}
// 需要返回值,使用submit callable接口实现
threadPool.shutdown();
}
2、CachedThreadPool:可缓存线程池
// 创建可缓存线程池
ExecutorService threadPool = Executors.newCachedThreadPool();
// 可缓存线程池的特点,无限大,如果线程中没有可用的线程则创建,有空闲则利用起来
for(int i = 0; i < 100; i++) {
// 执行runnable对象
final int index = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "---" + index);
}
});
}
// 需要返回值,使用submit callable接口实现
threadPool.shutdown();
3、SingleThreadPool:单线程池
4、ScheduledThreadPool:调度线程池
public static void main(String[] args) {
// 创建可调度线程池:定时执行任务
ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);
threadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(new Date() + "延迟执行1秒,每三秒执行一次");
}
}, 1, 3, TimeUnit.SECONDS);
}
需要的时候可以快速使用,不用重新收集
批量计算任务、服务器处理请求、excel解析
实际上,在开发中,如果需要创建5个以上的线程,那么就可以使用线程池来管理
(1)线程池构造方法的参数
参数名 | 类型 | 含义 |
---|---|---|
corePoolSize | int | 核心线程数 |
maxPoolSize | int | 最大线程数 |
keepAliveTime | long | 保持存活时间 |
workQueue | BlockingQueue | 任务存储队列 |
threadFactory | threadFactory | 当线程池需要新的线程的时候,会使用ThreadFactory来创建新的线程 |
Handler | RejectExecutionHandler | 当线程池无法接受你所提交的任务的拒绝策略 |
(2)增减线程的特点
(3)线程存活时间:keepAliveTime
如果线程池当前的线程数多于corePoolSize,那么如果多于的线程空闲时间超过keepAliveTime,它们就会被终止
(4)创建线程:ThreadFactory
默认使用Executors.defaultThreadFactory()
创建出来的线程都在同一个线程组
如果自己指定ThreadFactory,那么就可以改变线程名、线程组、优先级是否是守护线程等
通常情况下,使用默认的线程工厂基本上就可以满足绝大多数的需要
(5)工作队列:
有3种最常见的队列类型
CPU密集型(加密、计算hash等):最佳线程数为CPU核心数的1-2倍
耗时IO型(读写数据库、文件、网络读写等):最佳线程数一般会大于CPU核心数很多倍
参考Brain Goetz推荐的计算方法:
线程数 = CPU核心数*(1 + 平均等待时间/平均工作时间)
FixedThreadPool
核心线程池和最大线程数相同,队列满后,无法再增加线程
CachedThreadPool
具有自动回收多余线程的功能,队列没有容量,线程数可以很多
ScheduledThreadPool
支持定时及周期性任务执行的线程池
SingleThreadExecutor
只会用唯一的工作线程来执行任务(用到的场景并不多)
(5)停止线程池的正确方法:
1、shutdown
运行这个方法之后并不一定会停止,事实上,这个方法仅仅是初始化整个关闭过程。运行这个方法,会把正在执行的和队列中等待的都执行完毕之后再关闭,但是不会再增加任务。
2、isShutdown
3、isTerminated:线程都结束了返回true
4、awaitTermination
5、shutdownNow
任务太多,怎么拒绝:
4种拒绝策略:
钩子方法,给线程池加点料:
(1)线程池组成部分:
(2)Executor家族:
(1)execute方法:执行任务
(2)使用线程池的注意点:
public class ThreadLocalSample2 {
public static void main(String[] args) {
Service1 service1 = new Service1();
service1.process();
new Service2().process();
}
}
class Service1 {
public void process() {
User user = new User("三三");
UserHolderContext.holder.set(user);
}
}
class Service2 {
public void process() {
UserHolderContext.holder.get();
}
}
class UserHolderContext {
public static ThreadLocal<User> holder = new ThreadLocal<>();
}
class User {
String name;
public User(String name) {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1、让某个需要用到的对象在线程间隔离(每个线程都有自己独立的对象)
2、在任何方法中都可以轻松获取到该对象
场景一:initialValue
在ThreadLocal第一次get的时候把对象初始化出来,对象的初始化时机可以由我们控制
场景二:set
ThreadLocal里面对象的生成时机不由我们控制,例如拦截器生成的用户信息,用ThreadLocal.set直接放到ThreadLocal中,以便后续使用
使用ThreadLocal带来的好处:
达到线程安全
不需要加锁,提高执行效率
更高效地利用内存、节省开销
相比于每个任务都新建一个SimpleDateFormat,显然用ThreadLocal可以节省内存和开销
免去传参的繁琐
–不需要每次都传同样的参数
–ThreadLocal使得代码耦合度更低、更优雅
(1)initialValue() 初始化
(2)void set(T t):为这个线程设置一个新值
(3)T get():得到这个线程对应的Value,如果是首次调用get(),则会调用initialize来得到这个值
(4)void remove():删除对应这个线程的值
某个对象不再使用,但是占有的内存无法回收
(1)ThreadLocalMap的每个Entry都是一个对key的弱引用,同时,每个Entry都包含一个value的强引用
(2)正常情况下,当一个线程终止,保存在ThreadLocal里的value会被垃圾回收,因为没有任何强引用了
(3)但是,如果线程不终止(需要保持很久),那么key对应的value就不能被回收,这种情况下就会发生内存泄漏,就可能出现OOM
调用remove方法,就会删除对应的Entry对象,可以避免内存泄漏,所以使用ThreadLocal之后,应该调用remove()方法
【注:学习来源——慕课网】
1、@EnableAsyn
2、ThreadPool bean配置
3、方法上面加上@Aysnc(“threadPoolname”)