1.线程创建
Thread类:创建的线程都是一样的
1.继承Thread,重写run;
2.实现Runable,重写run;
3.使用匿名内部类,继承Thread;
4.使用匿名内部类,实现Runnable;
5.使用lambda表达式(常用);
public static void main(String[] args) {
Thread t = new Thread(() ->{
System.out.println("hello world");
});
t.start();
}
}
创建线程并命名:
Thread t2 = new Thread("这是我的名字");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello world");
}
},"这是我的名字");
2. 线程中断(interrupt)
中断一个线程:中断的意思是,不是让线程立即就停止,而是通知线程,你应该要停止了,是否真的停止,取决于线程这里具体的代码写法;
目前常见的有以下两种方式:
1. 通过共享的标记来进行沟通
private static boolean flag = true;
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() ->{
while(flag){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
Thread.sleep(3000);
//主线程里就可以随时通过操作flag变量的取值,来操作t线程是否结束
flag = false;
}
2. 调用 interrupt() 方法来通知
public class ThreadDemo5 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() ->{
while(!Thread.currentThread().isInterrupted()){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
Thread.sleep(3000);
//就可以终止线程了。
t.interrupt();
}
}
interrupt会做两件事:
1.把线程内部的标志位(boolean)给设置成true;
2.如果线程在进行sleep,就会触发异常,把sleep唤醒;
但是sleep在唤醒的时候,还会做一件事,把刚才设置的这个标志位,再设置回false.(情况了标致位)。
(1)加上break,立即中断:
public class ThreadDemo5 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() ->{
while(!Thread.currentThread().isInterrupted()){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
});
t.start();
Thread.sleep(3000);
//就可以终止线程了。
t.interrupt();
}
}
(2)加上睡眠时间,稍后进行中断:
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() ->{
while(!Thread.currentThread().isInterrupted()){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//稍后进行终止
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
break;
}
}
});
t.start();
Thread.sleep(3000);
//就可以终止线程了。
t.interrupt();
}
3.线程等待(join)
main线程等待t线程执行完后,再执行;
public static void main(String[] args) {
Thread t =new Thread(() ->{
for (int i = 0; i < 3; i++) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("join之前");
//此处的join就是让main线程来等待t线程执行结束(等待t的run执行完毕)
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("join之后");
}
4.线程休眠
让线程休眠:本质上就是让这个线程不参与调度了(不去CPU上执行了)。
线程的休眠:
1.一旦线程进入阻塞状态,对应PCB 就进入阻塞队列了,此时就暂时无法参与调度了.
2.比如调用sleep(1000),对应的线程PCB就要再阻塞队列中待1000ms 这么久;
3.当这个PCB回到了就绪队列,会被立即调度嘛? 不一定
虽然是sleep (1000),但是实际上考虑到调度的开销,对应的线程是无法在唤醒之后立即就执行的.实际上的时间间隔大概率要大于1000ms。
public static void main(String[] args) throws InterruptedException {
System.out.println(System.currentTimeMillis());
Thread.sleep(3 * 1000);
System.out.println(System.currentTimeMillis());
}
5.获取当前线程的引用
//获取当前线程的引用和休眠线程
public static void main(String[] args) throws InterruptedException {
System.out.println(System.currentTimeMillis());
Thread.sleep(3 * 1000);
System.out.println(System.currentTimeMillis());
Thread thread = Thread.currentThread();
System.out.println(thread.getName());
}
当前线程是main主线程。