基础概念
CPU核心数和线程数的关系
核心数:线程数=1:1 ;使用了超线程技术后—> 1:2
CPU时间片轮转机制(RR调度)
由操作系统控制,会导致上下文切换,花费5~2万时钟周期。
什么是进程和线程
进程:程序运行资源分配的最小单位,进程内部有多个线程,会共享这个进程的资源
线程:CPU调度的最小单位,必须依赖进程而存在。
澄清并行和并发
并行:同一时刻,可以同时处理事情的能力
并发:与单位时间相关,在单位时间内可以处理事情的能力
高并发编程的意义、好处和注意事项
好处:充分利用cpu的资源、加快用户响应的时间,程序模块化,异步化
问题:
线程共享资源,存在冲突;
容易导致死锁;
启用太多的线程,就有搞垮机器的可能
public class CheckThread {
public static void main(String[] args) {
//虚拟机线程管理的接口
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
for (ThreadInfo threadInfo : threadInfos) {
System.out.println(threadInfo.getThreadId()+": "+threadInfo.getThreadName());
}
}
}
从上面代码的运行结果可知:java程序天生就是多线程的
新启线程的三种方式:
继承Thread类:
public class ThreadTest {
private static class OneThread extends Thread{
@Override
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getId());
}
}
}
public static void main(String[] args) throws InterruptedException{
OneThread oneThread = new OneThread();
oneThread.start();
}
}
Runable接口:
public class RunableTest {
public static void main(String[] args) {
Runnable runnable=()->{
while (true) {
System.out.println(Thread.currentThread().getId() + ":" + Thread.currentThread().getName());
}
};
new Thread(runnable).start();
}
}
Callable接口(run方法中有返回值):
public class CallableTest {
public static void main(String[] args) throws InterruptedException, ExecutionException {
Callable<String> callable=()->{
return Thread.currentThread().getId()+": "+Thread.currentThread().getName();
};
//启动线程,先要将Callable包装为一个 Runable。而FutureTask实现了Runable接口
FutureTask<String> futureTask=new FutureTask<>(callable);
new Thread(futureTask).start();
String value = futureTask.get();//阻塞状态,直到Callable执行结束
System.out.println(value);
}
}
线程终止的正确方式:
自然终止的两种方式:1.执行结束,2.抛出异常
手动中断线程的方法:(都是Thread类提供的)
stop(),resume(),suspend()已不建议使用,stop()会导致线程不会正确释放资源,suspend()容易导致死锁。
java线程是协作式,而非抢占式
1.调用一个线程的interrupt() 方法中断一个线程,并不是强行关闭这个线程,只是跟这个线程打个招呼,将线程的中断标志位置为true,线程是否中断,由线程本身决定。
2.isInterrupted() 判定当前线程是否处于中断状态。
3.static方法interrupted() 判定当前线程是否处于中断状态,同时中断标志位改为false。
4.某个方法里如果抛出InterruptedException,线程的中断标志位会被复位成false,如果确实是需要中断线程,要求我们自己在catch语句块里再次调用interrupt()。
ex1:
public class StopThread {
private static class TwoThread extends Thread{
@Override
public void run() {
while (!isInterrupted()){
System.out.println(Thread.currentThread().getId()+":"+Thread.currentThread().getName());
}
}
}
public static void main(String[] args) throws InterruptedException{
TwoThread twoThread = new TwoThread();
twoThread.start();
Thread.sleep(500);
twoThread.interrupt();
}
}
ex2:
public class StopThread2 {
public static void main(String[] args) throws InterruptedException{
Runnable runnable=()->{
while (!Thread.currentThread().isInterrupted()){
System.out.println(Thread.currentThread().getId()+":"+Thread.currentThread().getName());
}
};
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(500);
thread.interrupt();
}
}
ex3:(某个方法里如果抛出InterruptedException,线程的中断标志位会被复位成false,如果确实是需要中断线程,要求我们自己在catch语句块里再次调用interrupt())
public class StopThread2 {
public static void main(String[] args) throws InterruptedException{
Runnable runnable=()->{
while (!Thread.currentThread().isInterrupted()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("11111111111111111111111");
Thread.currentThread().interrupt();
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().isInterrupted());
}
};
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(500);
thread.interrupt();
}
}
对Java里的线程再多一点点认识
线程常用方法和线程的状态
线程只有5种状态。整个生命周期就是这几种状态的切换。
run()和start() :run方法就是普通对象的普通方法,只有调用了start()后,Java才会将线程对象和操作系统中实际的线程进行映射,再来执行run方法。
yield() :让出cpu的执行权,将线程从运行转到可运行状态,但是下个时间片,该线程依然有可能被再次选中运行。
线程的优先级
取值为1~10,缺省为5,但线程的优先级不可靠,不建议作为线程开发时候的手段
守护线程
和主线程共死,finally不能保证一定执行
线程间的共享
synchronized内置锁
对象锁,锁的是类的对象实例。
类锁 ,锁的是每个类的的Class对象,每个类的的Class对象在一个虚拟机中只有一个,所以类锁也只有一个。
volatile关键字
适合于只有一个线程写,多个线程读的场景,因为它只能确保可见性,但不保证原子性。
ThreadLocal
线程变量。可以理解为是个map,类型 Map<Thread,Integer>