-
线程也被称为轻量型进程
-
每个线程对应一个独立的执行流,在这个执行流里就能完成一系列的指令
-
一个进程包含了多个线程。
-
一个线程出现问题,整个进程也可能会出现问题。
-
进程和线程的区别:
-
进程包含线程
-
进程是资源分配的基本单位,线程是系统调度执行的基本单位
-
进程和进程之间相互独立,互不影响;线程与线程会共享资源,某线程出问题可能会影响其它线程
-
-
JVM在启动时会划分若干个区域,其中方法区和堆,是多个线程共享的区域;栈和计数器,每个线程都有自己的区域。
-
站在操作系统的角度,进程的整个空间都由线程共享
通过继承手动创建线程
-
建立一个类继承自Thread,重写run方法
-
在run函数中写入执行流
-
在主函数中创建实例并通过start函数执行
-
为了方便观察引入sleep睡眠函数,但线程可能会提前苏醒,通过异常包裹。睡眠时间过后线程将重新抢占CPU时间片,若没抢占到则延后执行直至抢占到,所以等待线程的执行可能多于睡眠时间。
-
package Thread; class MyThread extends Thread{ @Override public void run() { while(true){ System.out.println("新线程"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Text { public static void main(String[] args) { MyThread myThread = new MyThread(); myThread.start(); while(true){ System.out.println("主线程"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
比较串行和多线程并发处理的时间(匿名创建线程)
package Thread;
public class ThreadDemo {
private static long count = 10_000_000_000L;
public static void serial(){
long start = System.currentTimeMillis();
long a =0;
for (long i = 0; i < count; i++) {
a++;
}
long b = 0;
for (long i = 0; i <count ; i++) {
b++;
}
long end = System.currentTimeMillis();
System.out.println("串行处理总计时间:"+(end-start)+"ms");
}
public static void concurrency(){
long start = System.currentTimeMillis();
Thread thread1 = new Thread(){
@Override
public void run() {
long a =0;
for (long i = 0; i < count; i++) {
a++;
}
}
};
thread1.start();
Thread thread2 = new Thread(){
@Override
public void run() {
long b = 0;
for (long i = 0; i <count ; i++) {
b++;
}
}
};
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("多线程并发处理总计时间:"+(end-start)+"ms");
}
public static void main(String[] args) {
serial();
concurrency();
}
}
根据输出结果可以观察到,多线程编程处理两百亿次数据用时缩短了将近一半时间。
通过实现Runnable接口,将Runnable实力赋值给Thread
package Thread;
class MyTask implements Runnable{
@Override
public void run() {
while(true){
System.out.println("hello");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadDemo2 {
public static void main(String[] args) {
Runnable MyTask = new MyTask();
Thread thread = new Thread(MyTask);
thread.start();
}
}
通过lambda表达式建立线程
public class ThreadDemo3 {
public static void main(String[] args) {
Thread thread = new Thread(()-> System.out.println("lambda表达式建立线程"));
thread.start();
}
}
Thread的其他方法
-
getState() 获取该线程的状态
-
getPriority()获取该线程的优先级
-
isDaemon()是否为后台线程,后台线程不会影响整个线程的结束,而前台线程会影响。
-
isAlive()判断当前线程是否结束
-
isInterrupted()判断是否被中断
Start和run方法的区别
执行start才会创建线程,执行新的执行流;惹通过run方法不创建线程,还是在原有的线程上执行。
中断线程的方法
-
线程的入口方法执行完毕
-
interrupt方法结束;针对线程代码改变:
-
由while条件里判断是否执行。
public class ThreadDemo4 { static boolean flag = true; public static void main(String[] args) { Thread thread = new Thread(){ @Override public void run() { while(flag){ System.out.println("线程正在运行"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); break; } } } }; System.out.println("线程即将运行"); thread.start(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程即将终止"); flag = false; } }
-
while条件设为!Thread.currentThread.isInterrupted(),在catch里加一个break,在想要停止执行的时间时加入thread.interrupt()方法,将状态设置为终止。
-
public class ThreadDemo4 {
public static void main(String[] args) {
Thread thread = new Thread(){
@Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
System.out.println("线程正在运行");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
};
System.out.println("线程即将运行");
thread.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程即将终止");
thread.interrupt();
}
}
终止时执行catch中的异常操作,之后break,退出线程的执行。
JOIN等待线程
-
每个线程执行的先后顺序是不确定的,完全取决与操作系统的调度。
-
等待机制能确定线程结束的先后顺序,谁调用join就等待谁结束。
-
join()等待线程结束
-
join(long millis)等待线程结束,最多等带millis毫秒
获取线程的引用
Thread.currentThread() 放回当前线程的引用
sleep休眠
线程存在多个状态,其中,就绪状态是可以执行但没抢占到CPU,而等待状态是还不可以执行的线程。
存在就绪队列和等待队列,当线程执行sleep时就会从就绪队列中调到等待队列中,休眠时间到了以后再从等待队列调回就绪队列,至于什么时候执行看什么时候抢占到CPU。