Runnable 和 Thread
Thread
类实现了 Runnable
接口,
run()
方法是 Runnable
接口中的抽象方法
线程相关概念
-
单线程:同一个时刻,只允许执行一个线程
-
多线程:同一个时刻,可以执行多个线程,比如:一个qq进程,可以同时打开多个聊天窗口,一个迅雷进程,可以同时下载多个文件
-
并发:同一个时刻,多个任务交替执行,造成一种“貌似同时”的错觉,简单的说,单核 cpu 实现的多任务就是并发。
-
并行:同一个时刻,多个任务同时执行。多核 cpu 可以实现并行。并发和并行可以同时实现。
通过Java代码获取当前电脑的 cpu 数量
package thread_;
/**
* @Author: Gin
* @Description:
* @Modified By: Gin
* @Date: Created in 16:26 2021/9/25
*/
public class CpuNums {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
// 获取当前电脑的 cpu 数量
int cpuNums = runtime.availableProcessors();
System.out.println("cpuNums = " + cpuNums);
}
}
线程基本使用
在 java
中线程来使用有两种方法:
- 继承
Thread
类,重写run
方法 - 实现
Runnable
接口,重写run
方法
继承 Thread 类
请编写程序开启一个线程,该线程每隔1秒。在控制台输出“GinSherry”
对上题改进:当输出 10 次后,结束该线程
package thread_;
/**
* @Author: Gin
* @Description:
* @Modified By: Gin
* @Date: Created in 17:04 2021/9/25
*/
public class Thread01 {
public static void main(String[] args) {
// 创建 Cat 对象,当作线程使用
Cat cat = new Cat();
// 启动线程
cat.start();
}
}
// 1. 当一个类继承了 Thread 类,该类就可以当作线程使用
// 2. 我们会重写 run 方法,写上自己的业务代码
// 3. run 方法来自 Thread 实现的 Runnable 接口
// Thread.java 中的 run 方法:
// @Override
// public void run() {
// if (target != null) {
// target.run();
// }
// }
class Cat extends Thread{
int times = 0;
@Override
public void run() {
while(true){
// 每隔一秒输出 ”GinSherry“
System.out.println("GinSherry" + ++times);
// 让该线程休眠 1 秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 执行 10 次就停止
if(times >= 10){
break;
}
}
}
}
多线程机制
package thread_;
/**
* @Author: Gin
* @Description:
* @Modified By: Gin
* @Date: Created in 17:04 2021/9/25
*/
public class Thread01 {
public static void main(String[] args) {
// 创建 Cat 对象,当作线程使用
Cat cat = new Cat();
// 启动线程
cat.start();
// 说明:当主线程启动一个子线程 Thread-0 ,主线程不会阻塞,会继续执行
// 这时,主线程和子线程是交替执行
System.out.println("主线程继续执行,主线程名 = " + Thread.currentThread().getName());
for (int i = 1; i <= 10; i++) {
System.out.println("主线程 i = " + i);
// 让主线程休眠
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Cat extends Thread{
int times = 0;
@Override
public void run() {
while(true){
// 每隔一秒输出 ”GinSherry“
System.out.println("GinSherry 子线程名 = " + Thread.currentThread().getName() + " 次数 = " + ++times);
// 让该线程休眠 1 秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 执行 10 次就停止
if(times >= 10){
break;
}
}
}
}
为何要调用 cat.start(),而不直接调用 cat.run()
package thread_;
/**
* @Author: Gin
* @Description:
* @Modified By: Gin
* @Date: Created in 17:04 2021/9/25
*/
public class Thread01 {
public static void main(String[] args) {
// 创建 Cat 对象,当作线程使用
Cat cat = new Cat();
// 启动线程
// cat.start();
// 如果不通过 cat.start 来执行 run 方法,而是直接调用 run 方法
// 则 run 方法就是一个普通的方法,并不会真正启动一个线程,方法会阻塞,直到执行完 run 方法,才会继续向下执行
// 可通过输出的子线程名来验证
cat.run();
// 说明:当主线程启动一个子线程 Thread-0 ,主线程不会阻塞,会继续执行
// 这时,主线程和子线程是交替执行
System.out.println("主线程继续执行,主线程名 = " + Thread.currentThread().getName());
for (int i = 1; i <= 10; i++) {
System.out.println("主线程 i = " + i);
// 让主线程休眠
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 1. 当一个类继承了 Thread 类,该类就可以当作线程使用
// 2. 我们会重写 run 方法,写上自己的业务代码
// 3. run 方法来自 Thread 实现的 Runnable 接口
// Thread.java 中的 run 方法:
// @Override
// public void run() {
// if (target != null) {
// target.run();
// }
// }
class Cat extends Thread{
int times = 0;
@Override
public void run() {
while(true){
// 每隔一秒输出 ”GinSherry“
System.out.println("GinSherry 子线程名 = " + Thread.currentThread().getName() + " 次数 = " + ++times);
// 让该线程休眠 1 秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 执行 10 次就停止
if(times >= 10){
break;
}
}
}
}
start() 方法底层
/*
源码解读
1.
public synchronized void start() {
start0();
}
2. start0() 是本地方法,是 JVM 机调用,底层是 c/c++ 实现
真正实现多线程效果的是 start0(),而不是 run()
start()方法调用start0()方法后,该线程并不一定会立马执行,只是将线程变成了可运行状态。具体什么时候执行,取决于cpu,由cpu统一调度。
private native void start0();
*/
实现 Runnable 接口
package thread_;
/**
* @Author: Gin
* @Description:
* @Modified By: Gin
* @Date: Created in 19:57 2021/9/25
*/
public class Thread02 {
public static void main(String[] args) {
Dog dog = new Dog();
Thread thread = new Thread(dog);
thread.start();
}
}
class Dog implements Runnable{
int count = 0;
@Override
public void run() {
while(true){
System.out.println("hi~~~ " + ++count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count >= 10){
break;
}
}
}
}
多个子线程案例
package thread_;
/**
* @Author: Gin
* @Description:
* @Modified By: Gin
* @Date: Created in 20:04 2021/9/25
*/
public class Thread03 {
public static void main(String[] args) {
T1 t1 = new T1();
T2 t2 = new T2();
Thread thread1 = new Thread(t1);
Thread thread2 = new Thread(t2);
thread1.start();
thread2.start();
}
}
class T1 implements Runnable{
int count = 0;
@Override
public void run() {
while(true){
// 每隔 1 秒输出一次 Hello~~~~,输出 10 次就停止
System.out.println("Hello~~~~" + ++count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count >= 10){
break;
}
}
}
}
class T2 implements Runnable{
int count = 0;
@Override
public void run() {
while(true){
// 每隔 0.5 秒输出一个 Hi~~~~,输出 5 次就停止
System.out.println("Hi~~~~" + ++count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count >= 5){
break;
}
}
}
}
继承 Thread 类和实现 Runnable 接口的区别
- 从
Java
的设计来看,通过继承Thread
或者实现Runnable
接口来创建线程本质上没有区别,从jdk
帮助文档我们可以看到Thread
类本身就实现了
Runnable
接口 - 实现
Runnable
接口方式更加适合多个线程共享一个资源的情况,并且避免了单继承的限制,建议使用Runnable
通过实现 Runnable 接口,两个线程都运行 t3 的 run 方法:
但是继承 Thread 类,是通过类对象来启动子线程,一个线程对应一个对象,不能实现资源共享: