- 线程创建
- 线程中断
- 线程等待
- 线程休眠
- 线程状态
- 线程实例
一.线程的创建---这里给出五种方法
方法一:
继承Thread 重写run方法
class MyThread extends Thread{
@Override
public void run() {
System.out.println("thread");
}
}
public class Text1 {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
方法二:
实现MyRunnable接口,重写run
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("thread");
}
}
public class Text2 {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
方法三:
使用匿名内部类 实现创建Thread子类
public class Text3 {
public static void main(String[] args) {
Thread thread = new MyThread(){
@Override
public void run() {
System.out.println("thread");
}
};
thread.start();
}
}
方法四:
使用匿名内部类 实现 实现Runnable接口
public class Text4 {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread");
}
});
thread.start();
}
}
方法五:
lambda 表达式
public class Text5 {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
System.out.println("thread");
});
thread.start();
}
}
注意一下,线程真正的创建是thread.start();
二.线程中断
线程中断--其实是让线程快一点结束(run执行完了,线程就执行完了),不是让它半路停下!
介绍两个方法
1.自己设置一个 -举个栗子~~
public class Text6 {
private static boolean t = true;
public static void main(String[] args) {
Thread thread = new Thread(() ->{
while(t){
System.out.println("thread");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程执行完了");
});
thread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t = false;
System.out.println("设置线程执行完了");
}
}
2.用标准库里自带的
public class Text7 {
public static void main(String[] args) {
Thread thread = new Thread(() ->{
while(!Thread.currentThread().isInterrupted()){
System.out.println("thread");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//break;
}
}
System.out.println("线程执行完了");
});
thread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.interrupt();
System.out.println("设置线程执行完了");
}
}
我们发现它还是没停下来,没关系,加一个break就行
从中我们也能发现Java中断线程也不是强制的,它是 立即处理,不理会,装作不理会 (像回消息~~)
三.线程等待
用join 在main函数中调用thread.join效果就是--main函数阻塞等待,thread线程执行完了,main才继续
public class Text8 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while(true){
System.out.println("thread");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
thread.join();
System.out.println("main等待");
while(true){
System.out.println("main");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
四.线程休眠
线程休眠就是sleep,调用了则这个PCB就被移动到,另外一个 '阻塞队列' 中,时间到了,就在回到, '就绪队列' 的原来位置
五线程状态
NEW | 安排了工作, 但还未开始 |
RUNNABLE | 可工作 (正在工作中和即将开始工作) |
BLOCKED | 排队等着其他事情 |
WAITING | 排队等着其他事情 |
TIMED_WAITING | 排队等着其他事情 |
TERMINATED | 工作完成了 |
简单来说:
public class Text9 {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (int i = 0; i < 50; i++) {
}
});
System.out.println(thread.getState());
thread.start();
while(thread.isAlive()){
System.out.println(thread.getState());
}
System.out.println(thread.getState());
}
public class Text {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i = 0; i < 3; i++) {
System.out.println("thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
System.out.println(thread.getState());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(thread.getState());
thread.join();
System.out.println(thread.getState());
}
}
六.线程实例
public class Text10 {
public static void main(String[] args) {
Thread thread = new MyThread(){
@Override
public void run() {
System.out.println(this.getName());
}
};
thread.start();
}
}