Thread 类的基本用法
创建线程的五种方法:
继承 Thread, 重写 run
class MyThread extends Thread{//继承
@Override//重写
public void run() {
while(true) {
System.out.println("hello");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {//中断异常
e.printStackTrace();
}
}
}
}
public class ThreadDemo {
public static void main(String[] args) {//主线程
Thread t = new MyThread();
t.start();//新线程
while(true){
System.out.println("main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
实现 Runnable, 重写 run
class MyRunnable implements Runnable {
@Override
public void run() {
while(true) {
System.out.println("hello t");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadDemo2 {
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable();
Thread t = new Thread(runnable);
t.start();
while(true) {
System.out.println("main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
继承 Thread, 重写 run, 使用匿名内部类
public class ThredDemo3 {
public static void main(String[] args) {
Thread t = new Thread() {
//匿名内部类
@Override
public void run() {
while(true) {
System.out.println("hello t");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
while(true) {
System.out.println("main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
实现 Runnable, 重写 run, 使用匿名内部类
public class ThreadDemo4 {
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while(true) {
System.out.println("hello t");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
while(true) {
System.out.println("main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
使用 lambda 表达式
public class ThreadDemo5 {
public static void main(String[] args) {
Thread t = new Thread(() ->{
while (true) {
System.out.println("hello t");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
while(true){
System.out.println("main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程的中断
让线程的入口方法执行完毕
给线程中设定一个结束标志位
public class TestDemo2 {
public static boolean isQuit = false;
public static void main(String[] args) {
Thread thread = new Thread(()->{
while(!isQuit){
System.out.println("hello t");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("END");
});
thread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
isQuit = true;
}
}
运用interrupt
Thread.currentThread().isInterrupted()
( isInterrupted 标志位 = false)
( intrerupt = ture)
(!false)-> (! true)
public class TestDemo2 {
//public static boolean isQuit = false;
public static void main(String[] args) {
Thread thread = new Thread(()->{
while(!Thread.currentThread().isInterrupted()){
//isInterrupted 标志位 = false
System.out.println("hello t");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//抛出异常
break;
}
}
System.out.println("END");
});
thread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
//打印出异常位置的调用栈
}
// isQuit = true;
thread.interrupt();
//清空标志位为 ture
}
}
线程的等待
有时,我们需要等待一个线程完成它的工作后,才能进行自己的下一步工作。
(join 方法)使该线程 阻塞 等待
public class TestDEMO3 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
System.out.println("hello t");
});
thread.start();
thread.join();
System.out.println("main");
}
}
让 start 创建的新线程等待 hello t 打印完后 再执行
获取线程实例
你在哪个线程中去调用这个方法,那么它就会返回这个线程的实例对象.
public class ThreadDemo11 {
public static void main(String[] args) {
Thread t = new Thread(() -> {
System.out.println(Thread.currentThread().getName());
},"1线程");
t.start();
}
}