1. 线程创建
一般线程的创建有五种
1.1 继承Thread, 重写 run 方法
class MyThread extends Thread{
public void run() {
while (true) {
System.out.println("holle,z");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadDemo1 {
public static void main(String[] args) {
Thread t = new MyThread();
t.start();
while (true){
System.out.println("holle,l");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
1.2 实现 Runnable 接口,重写 run
class MyRunnable implements Runnable {
@Override
public void run() {
while (true){
System.out.println("holle,z");
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("holle,l");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
1.3 继承 Thread ,重写 run ,使用内部类
public class ThreadDemo3 {
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run() {
while (true){
System.out.println("holle,z");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
while (true){
System.out.println("holle,l");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
1.4 实现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("holle,z");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
while (true){
System.out.println("holle,l");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
1.5 lambda 表达式
public class ThreadDemo5 {
public static void main(String[] args) {
Thread t = new Thread( () -> {
while (true){
System.out.println("holle,z");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
while (true){
System.out.println("holle,l");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2. 线程中断
顾名思义就是线程执行完毕
1. 自己手动设置一个标志位(变量boolean)终止线程
多个线程使用同一份内存地址,故 main 和 t 线程判定同一个 标志位(isquit)
2. 调用 Thread 自己的标志位
Thread.interrupted()
调用这个方法会有两种情况
1. 线程是就绪状态,把标志位 置为 true
2. 如果线程的阻塞状态就会出发异常
public static void main(String[] args){
Thread t = new Thread(() -> {
System.out.println("bit");
//currentThread()获取当前对象实例
//此处currentThread获取的对象是 t
//isInterrupted 是t 对象自带的标志位
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
});
//System.out.println(t.getName());
t.start();
//t.join();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
System.out.println("holle");
}
3. 线程等待
顾名思义,让其中一个 线程先执行,其余的线程等待
public static void main(String[] args) throws InterruptedException{
Thread t = new Thread(() -> {
System.out.println("bit");
//currentThread()获取当前对象实例
//此处currentThread获取的对象是 t
//isInterrupted 是t 对象自带的标志位
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
});
t.start();
t.join();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//t.interrupt();
System.out.println("holle");
}
这里就是 t 线程先执行,然后执行main 里面的线程
4. 线程休眠
public static void main(String[] args) throws InterruptedException{
Thread t = new Thread(() -> {
System.out.println("bit");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
});
t.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("holle");
}
这里 t 线程 休眠了一秒打印 bit ,随后 main 里面的线程休眠了两秒打印 holle
5. 获取线程实例
public static void main(String[] args) {
Thread t = new Thread(() -> {
System.out.println(Thread.currentThread());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t.start();
}