CPU核心数和线程的关系
核心数:线程数 = 1:1 超线程技术-> 1:2
CPU时间片轮转机制
RR调度 上下文切换
什么是进程和线程
进程:程序运行资源分配的最小单位,进程内部有多少个线程,会共享这个进程的资源
线程:CPU调度的最小单位
并行和并发
并发:与时间单位,在单位时间内处理事情的能力
并行:同一时刻可以处理事情的能力
Java里的程序天生就是多线程的,有几种新启线程的方式
①继承Thread
public class UseThread {
private int ticket = 10;
public void saleTicket(){
while (ticket > 0){
this.ticket--;
System.out.println(this.ticket);
}
}
public static void main(String[] args){
UseThread ut = new UseThread();
for (int i = 0; i < 2; i++) {
new Thread(()->{
ut.saleTicket();
}).start();
}
}
}
②接口Runable
实现Runnable接口,无返回值
public class UseRun implements Runnable{
@Override
public void run() {
System.out.println("I am implements Runnable");
}
public static void main(String[] args) {
UseRun useRun = new UseRun();
Thread t1 = new Thread(useRun);
t1.start();
}
}
③ 接口Callable
实现Callable接口,允许有返回值
public class UseCall implements Callable<String>{
@Override
public String call() throws Exception {
System.out.println("I am implements Callable");
return "Callable";
}
public static void main(String[] args) throws ExecutionException, InterruptedException{
UseCall useCall = new UseCall();
FutureTask<String> futureTask = new FutureTask<>(useCall);
new Thread(futureTask).start();
System.out.println(futureTask.get());
}
}
有开始就有结束,怎么样才能让Java的线程安全停止工作呢?
Stop()还是Interrupt()、isinterrupted()、static方法interrupted(),深入理解这些方法
- 自然执行完,2抛出异常
stop(), resume(),suspend()线程不会释放资源
线程是协作式
Interrupt() 中断一个线程,并不是强行关闭这个线程,打个招呼,中断标志位置为true,
isinterrupted()判定当前线程是否处于中断状态
static方法interrupted() 判定当前线程是否处于中断状态,中断标志位改为false
public class EndThread {
private static class UseThread extends Thread{
public UseThread(String name){
super(name);
}
@Override
public void run() {
String threadName = Thread.currentThread().getName();
while (!isInterrupted()){
System.out.println(threadName + "is running");
}
System.out.println(threadName+ "interrupted flag is" + isInterrupted());
}
}
public static void main(String[] args) throws InterruptedException {
Thread endThread = new Thread("endThread");
endThread.start();
Thread.sleep(20);
endThread.interrupt();
}
}
private static class UseRunnable implements Runnable{
@Override
public void run() {
String threadName = Thread.currentThread().getName();
while (Thread.currentThread().isInterrupted()){
System.out.println(threadName + "is run");
}
System.out.println(threadName + "interrupted is"
+Thread.currentThread().isInterrupted());
}
}
public static void main(String[] args) throws InterruptedException {
UseRunnable useRunnable = new UseRunnable();
Thread endThread = new Thread(useRunnable,"endThread");
endThread.start();
Thread.sleep(20);
endThread.interrupt();
}
}
某个方法会抛出InterruptedException,线程的中断标志位会被复位成false,需要我们自己在catch里再次中断
public class HasInterruptedException {
public static class UseThread extends Thread{
public UseThread(String name){
super(name);
}
@Override
public void run() {
String threadName = Thread.currentThread().getName();
while(!isInterrupted()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(threadName + "interrupted flag is "
+ isInterrupted());
interrupt();
e.printStackTrace();
}
System.out.println("while " + threadName);
}
System.out.println("run" +threadName);
}
}
public static void main(String[] args) throws InterruptedException {
UseThread useThread = new UseThread("HasInterruptedException");
useThread.start();
Thread.sleep(30);
useThread.interrupt();
}
}