Java中怎么创建一个线程。
1、继承Thread类并重写run()方法
public class MyThread extends Thread{
public void run(){
//此处为线程工作代码
}
}
2、实现Runnable接口并实现接口中的run()方法
public class MyThread implements Runnable{
@Override
public void run() {
// 线程的工作代码
}
}
3、Java8 函数式编程创建线程的方式
Thread thread = new Thread(()->{
//线程工作代码
});
4、线程的启动
Java中线程的启动据笔者目前所接触的而言,仅有一种方式:那就是调用Thread类的start()方法。
5、线程的停止
对于线程的停止,需要了解以下几个方法:
thread.interrupt();
thread.interrupted();
thread.isInterrupted();
thread.interrupt();
thread.interrupted();
thread.isInterrupted();
thread.interrupt():
给线程下发中断指令。该中断指令表示的并不是直接中断线程,而是可以理解为,给线程添加中断信号。向程序表明我希望或者说我想要中断线程。此操作并不会中止线程的运行,仅仅只是添加一个信号标记而已。
给线程下发中断指令。该中断指令表示的并不是直接中断线程,而是可以理解为,给线程添加中断信号。向程序表明我希望或者说我想要中断线程。此操作并不会中止线程的运行,仅仅只是添加一个信号标记而已。
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for(int i = 0; i < 100000; i++)
System.out.println("i = " + i);
});
thread.start();
Thread.sleep(100);
//中断线程
thread.interrupt();
System.out.println("main线程结束...");
}
执行上述代码,可以看出线程在执行thread.interrupt()方法时并不会停止线程的执行,甚至你丝毫感受不到这个方法对线程有什么影响。因为该方法仅仅只是向线程标识了一个中断信号,仅仅只是标记了一个想要结束线程的信号而已。
thread.isInterrupted()方法为判断线程是否已经处于中断状态。
简单的说,该方法就是判断有没有给线程注入中断标记,是否调用了该线程的interrupt()方法。
thread.interrupted()方法为判断调用this.interrupted()方法的线程是否处于中断状态。
简单的说,该方法就是判断有没有给线程注入中断标记,是否调用了该线程的interrupt()方法。
thread.interrupted()方法为判断调用this.interrupted()方法的线程是否处于中断状态。
public static void main(String[] args) throws InterruptedException {
MyThread1 t = new MyThread1();
t.start();
//调用该方法的线程为main
System.out.println("main : " + t.interrupted());
t.interrupt();
//调用该方法的线程为main
System.out.println("main : " + t.interrupted());
}
private static class MyThread1 extends Thread{
@Override
public void run() {
while(true) {
//调用该方法的线程为MyThread1
if(this.interrupted()) {
System.out.println("MyThread : 线程中断");
break;
} else {
System.out.println("MyThread : 线程运行");
}
}
}
}
所以该方法判断的是调用者的中断状态。
其中需要注意的是该方法会清除中断状态信息。
public static void main(String[] args) {
Thread thisThread = Thread.currentThread();
//第一次输出为false 因为还没有注入中断信息
System.out.println(thisThread.interrupted());
//注入中断信息
thisThread.interrupt();
//第二次输出为true 因为已经注入中断信息
System.out.println(thisThread.interrupted());
//第三次输出为false 因为第二次调用interrupted方法时,方法清除了中断状态。
System.out.println(thisThread.interrupted());
}
break方法中断线程
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.interrupt();
}
private static class MyThread extends Thread{
@Override
public void run() {
while(true) {
/****
*
* 采用break跳出循环结束线程
* 该方法的缺点是并不会立即结束线程。
* 因为如果循环外面还有执行代码的话,那么则会在外面的执行代码运行结束之后,才会中止线程
*/
if(this.interrupted()) {
System.out.println("线程中止了...");
break;
}
}
System.out.println("还没有哦,我还可以运行哦");
}
}
return方法结束线程
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.interrupt();
}
private static class MyThread extends Thread{
@Override
public void run() {
while(true) {
/****
* return会立即结束方法的执行,跳出run方法之后,线程也会结束执行。
* 该方法的缺点就是其他线程并不知道该线程已经结束。
*/
if(this.interrupted()) {
return;
}
}
}
}
异常法结束线程
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.interrupt();
}
private static class MyThread extends Thread{
@Override
public void run() {
while(true) {
/****
* 该方法中断线程是笔者最为推荐的方式来结束线程。
* 因为采用该方法来结束线程,结束信息会得到传播
* 其他线程也能知道该线程是否已经结束。
*/
if(this.interrupted()) {
throw new RuntimeException();
}
}
}
}