前言
本篇博客将从以下几个方面详解Thread类及Java中线程的几种状态
一.Thread类
1.1创建线程
方法一 继承Thread类
class MyThread extends Thread {
@Override
public void run() {
System.out.println("这⾥是线程运⾏的代码");
}
}
MyThread t = new MyThread();
t.start();
方法二 实现runnable 接口
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("这⾥是线程运⾏的代码");
}
}
Thread t = new Thread(new MyRunnable());
t.start();
方法三 使用匿名内部类创建Thread子类对象
// 使⽤匿名类创建 Thread ⼦类对象
Thread t1 = new Thread() {
@Override
public void run() {
System.out.println("使⽤匿名类创建 Thread ⼦类对象");
}
};
方法四 使用匿名内部类创建Runnable子类对象
// 使⽤匿名类创建 Runnable ⼦类对象
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("使⽤匿名类创建 Runnable ⼦类对象");
}
});
方法五 使用lambda表达式创建Runnable 子类对象
public class ThreadDemo6 {
public static void main(String[] args) {
Thread t = new Thread(() -> {
while (true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t.start();
while (true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
1.2中断线程
例:
public class ThreadDemo {
private static class MyRunnable implements Runnable {
public volatile boolean isQuit = false;
@Override
public void run() {
while (!isQuit) {
System.out.println(Thread.currentThread().getName()
+ ": 别管我,我忙着转账呢!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()
+ ": 啊!险些误了⼤事");
}
}
public static void main(String[] args) throws InterruptedException {
MyRunnable target = new MyRunnable();
Thread thread = new Thread(target, "李四");
System.out.println(Thread.currentThread().getName()
+ ": 让李四开始转账。");
thread.start();
Thread.sleep(10 * 1000);
System.out.println(Thread.currentThread().getName()
+ ": ⽼板来电话了,得赶紧通知李四对⽅是个骗⼦!");
target.isQuit = true;
}
}
示例二 使用Thread.interrupted()或者Thread.currentThread().isInterrupted()代替自定义标志位
注:Thread 内部包含了⼀个 boolean 类型的变量作为线程是否被中断的标记.
public class ThreadDemo {
private static class MyRunnable implements Runnable {
() {
@Override
public void run
// 两种⽅法均可以
while (!Thread.interrupted()) {
//while (!Thread.currentThread().isInterrupted()) {
System.out.println(Thread.currentThread().getName()
+ ": 别管我,我忙着转账呢!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(Thread.currentThread().getName()
+ ": 有内⻤,终⽌交易!");
// 注意此处的 break
break;
}
}
System.out.println(Thread.currentThread().getName()
+ ": 啊!险些误了⼤事");
}
}
public static void main(String[] args) throws InterruptedException {
MyRunnable target = new MyRunnable();
Thread thread = new Thread(target, "李四");
System.out.println(Thread.currentThread().getName()
+ ": 让李四开始转账。");
thread.start();
Thread.sleep(10 * 1000);
System.out.println(Thread.currentThread().getName()
+ ": ⽼板来电话了,得赶紧通知李四对⽅是个骗⼦!");
thread.interrupt();
}
}
1.3等待线程
我们一般用join()这个方法
public class ThreadDemo {
public static void main(String[] args) throws InterruptedException {
Runnable target = () -> {
for (int i = 0; i < 10; i++) {
try {
System.out.println(Thread.currentThread().getName()
+ ": 我还在⼯作!");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + ": 我结束了!")
};
Thread thread1 = new Thread(target, "李四");
Thread thread2 = new Thread(target, "王五");
System.out.println("先让李四开始⼯作");
thread1.start();
thread1.join();
System.out.println("李四⼯作结束了,让王五开始⼯作");
thread2.start();
thread2.join();
System.out.println("王五⼯作结束了");
}
}
注:run()方法和start()方法的区别
1.run()方法是线程执行的代码块,只有在start()方法被调用后才会执行。如果直接调用run()方法,实际上就是普通的方法调用,不会开启新的线程,而是在当前线程中运行这个方法;
2.start()方法是开启线程的方法,它会创建一个新的线程,并在新的线程中调用run()方法;
3.在多线程编程中,如果直接调用run()方法,那么所有的操作都是在主线程中完成的,而不是在新的线程中执行,这就不符合多线程编程的目的,因此,应该调用start()方法来开启新的线程,以实现多线程编程的目的。
1.4休眠线程
代码如下:
public class ThreadDemo {
public static void main(String[] args) throws InterruptedException {
System.out.println(System.currentTimeMillis());
Thread.sleep(3 * 1000);
System.out.println(System.currentTimeMillis());
}
}
1.5获取当前线程
代码如下:
public class ThreadDemo {
public static void main(String[] args) {
Thread thread = Thread.currentThread();
System.out.println(thread.getName());
}
}
二.线程中的几种状态
NEW:安排了工作,还未开始行动
RUNNABLE:可工作的,又可以分成正在工作中和即将开始工作
BLOCKED: 这几个都表示等着其他事情
WAITING:这几个都表示等着其他事情
TIMED_WAITING: 这几个都表示等着其他事情
TERMINATED:工作完成了
尾语
这篇博客到这里就结束啦,希望可以给大家带来帮助~~~