持续学习&持续更新中…
守破离
【Java从零到架构师第1季】【并发 Concurrent 01】线程基础
进程(Process)线程(Thread)线程的串行多线程多线程的原理多线程的优缺点默认线程开启新线程
第一种方法:实现Runnable接口第二种方法:继承Thread类 设置线程的优先级Java程序的内存划分多线程的内存布局线程的状态线程的状态切换sleep、interruptjoin、isAlive参考
进程(Process) 线程(Thread) 线程的串行在一个线程里面,是不能多个任务同时执行的。
多线程 多线程的原理 多线程的优缺点如果CPU是多核的话,那么使用多线程就可以充分利用这些核,这样就真的可以提高程序的执行效率。
默认线程程序一启动,默认开启的那个线程就是主线程。
开启新线程第一种方法:实现Runnable接口要想开启新线程应该调用Thread的start方法,而不是调用run方法。(Thread的start方法内部会去调用run方法)
直接调用Thread的run方法,相当于调用一个普通方法,并不会开启新线程。
public class ResourcesThread implements Runnable {
@Override
public void run() {
System.out.println("加载资源----ResourcesThread");
}
}
public static void main(String[] args) {
Thread thread = new Thread(new ResourcesThread());
thread.start();
}
当然也可以这样写:
原理:
第二种方法:继承Thread类public class MusicThread extends Thread {
public MusicThread() {
}
public MusicThread(String name) {
super(name);
}
@Override
public void run() {
System.out.println("播放音乐----" + getName());
}
}
public static void main(String[] args) {
Thread thread = new MusicThread("music");
thread.start();
}
当然也可以这样写:
public static void main(String[] args) {
Thread thread = new Thread() {
@Override
public void run() {
System.out.println("new thread");
}
};
thread.start();
}
设置线程的优先级
public static void main(String[] args) {
Thread threadMax = new Thread(() -> {
System.out.println(Thread.currentThread());
}, "threadMax");
Thread threadMin = new Thread(() -> {
System.out.println(Thread.currentThread());
}, "threadMin");
Thread threadNorm = new Thread(() -> {
System.out.println(Thread.currentThread());
}, "threadNorm");
threadMax.setPriority(Thread.MAX_PRIORITY);
threadMin.setPriority(Thread.MIN_PRIORITY);
threadNorm.setPriority(Thread.NORM_PRIORITY);
threadMax.start();
threadMin.start();
threadNorm.start();
}
Java程序的内存划分
多线程的内存布局
从“多线程的内存布局”我们就可以看出,开启新线程是有一定的代价的,因为每一条线程都需要被分配属于自己的PC寄存器、Java虚拟机栈、本地方法栈等。
线程的状态State源码:
public enum State {
NEW, // 新建状态:尚未启动(还未调用start方法)
RUNNABLE, // 可运行状态:有可能在等待CPU的调度,也有可能正在运行。(已经调用过了start方法)
BLOCKED, // 阻塞状态:在等待锁
WAITING, // 在等待其它的线程
TIMED_WAITING, // 在定时等待其它的线程
TERMINATED; // 终止状态:已经执行完毕。
}
测试:
public static void main(String[] args) {
Thread mainThread = Thread.currentThread();
Thread otherThread = new Thread("other thread") {
@Override
public void run() {
System.out.println("我是其它线程");
}
};
System.out.println(mainThread.getState()); // RUNNABLE
System.out.println(otherThread.getState()); // NEW
}
public static void main(String[] args) {
Thread otherThread = new Thread("other thread") {
@Override
public void run() {
System.out.println("我是其它线程");
}
};
System.out.println(otherThread.getState()); // NEW
otherThread.start();
System.out.println(otherThread.getState()); // RUNNABLE
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(otherThread.getState()); // TERMINATED
}
线程的状态切换
sleep、interrupt
通过Thread.sleep方法暂停当前线程,当前线程会进入 TIMED_WAITING 状态。
public static void main(String[] args) {
Thread thread = new Thread() {
@Override
public void run() {
System.out.println(1);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// 如果线程(在sleep的时候)被干扰(阻断、中断),就会出现该异常
// 如果线程被“吵醒”的话,那么该线程就不会继续sleep了,其它(下面)的代码就会立即得到执行
// 线程一旦被“吵醒”,那么它就该干活了
System.out.println(Thread.currentThread().getName() + "线程正在sleep时,被吵醒(打扰、阻断、中断)了");
}
System.out.println(2);
}
};
thread.start();
// 让main方法睡1秒钟,以确保上面的thread得到运行
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.interrupt(); // 阻断、中断线程sleep(叫该线程“起床”)
}
join、isAlive
测试1:
public static void main(String[] args) {
Thread thread = new Thread() {
@Override
public void run() {
System.out.println(1);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(2);
}
};
thread.start();
try {
// 让线程(调用join方法的线程)“插队”
// 让调用join方法的线程执行完后再继续执行当前线程(main线程)(main线程是当前上下文线程)
thread.join();
// thread.join(1000); // 当前线程(main线程)会等你1秒,如果1秒之后你还没有执行完,那么当前线程就不等了。
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(3);
System.out.println(thread.getState()); // TERMINATED
System.out.println(thread.isAlive()); // false
}
测试2:
public static void main(String[] args) {
Thread t1 = new Thread() {
@Override
public void run() {
System.out.println("t1-begin");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t1-end");
}
};
Thread t2 = new Thread() {
@Override
public void run() {
System.out.println("t2-begin");
System.out.println("t1.state: " + t1.getState());
System.out.println("t1.isAlive: " + t1.isAlive());
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t1.state: " + t1.getState());
System.out.println("t1.isAlive: " + t1.isAlive());
System.out.println("t2-end");
}
};
t1.start();
t2.start();
}
参考
小码哥-李明杰: Java从0到架构师①零基础高效率入门.
本文完,感谢您的关注支持!



