目录
一、Thread类
二、线程创建
三、线程中断
四、线程等待
五、线程休眠
六、获取线程实例
一、Thread类
在java标准库中,提供了一个Thread类,用来表示/操作线程,Thread类可以视为是Java标准库提供的API,Java是支持多线程编程的,在Java中创建好的Thread实例,其实和操作系统中的线程是一一对应的关系,操作系统提供了一组关于线程的API(C语言),Java对于这组API进一步封装之后就变成了Thread类
二、线程创建
- 继承 Thread, 重写 run
public static void main(String[] args) {
//继承的写法1.自定义一个类来继承
MyThread myThread = new
myThread.start();
//继承的写法2.使用一个匿名内部类
Thread t = new Thread(){//属于继承Thread但没有名称的子类
@Override
public void run() {
System.out.println("匿名内部类 run");
}
};
t.start();
}
//继承的方式:1.继承Thread,2.重写run方法(定义要执行的任务代码)
private static class MyThread extends Thread{
@Override
public void run() {
System.out.println("mythread run");
}
}
} - 实现 Runnable, 重写 run,使用 lambda 表达式
public class 实现Runnable {
public static void main(String[] args) {
//写法1
Thread t1 = new Thread(new MyRunnable());
t1.start();
//写法2:匿名内部类
Runnable r2 = new Runnable() {
@Override
public void run() {
System.out.println("匿名内部类run");
}
};
Thread t2 = new Thread(r2);
t2.start();
//写法3:把匿名内部类对象,直接写在构造方法的参数上
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("匿名内部类run");
}
});
t3.start();
//写法4:lambda表达式
Thread t4 = new Thread(() -> System.out.println("匿名内部类run"));
t4.start();
}
private static class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("MyRunnable run");
}
}
}
三、线程中断
- 设置标志位
public class 自定义标志位 {
//先设计一个标志位:表示是否被中断。
private static volatile boolean 是否被中断 = false;
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
//循环10次,每次打印次数,并休眠1秒
try {
//判断条件加上标志位
for (int i = 0; i < 10 && !是否被中断; i++) {
System.out.println(i);
//自定义标志位,能够实现某些条件的中断
//但如果线程处于等待/超时等待/阻塞, 就没法中断
//修改时间为100秒就没法中断
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
//要中断t线程:等3后再中断
Thread.sleep(3000);
是否被中断 = true;
}
} - Thread类内部包含了一个boolean的变量可以用来作为标记位,标记是否被中断Thread.interrupted()或者Thread.currentThread().isInterrupted()都可以
public class demo {
public static void main(String[] args) {
Thread t = new Thread() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
};
t.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
}
} - Thread.interrupted()线程中断会清除标记位,Thread.currentThread().isInterrupted()线程标记位不会被清空
public class IsInterrupted {
public static void main(String[] args) {
Thread thread1 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.interrupted());
}
}
};
thread1.start();
thread1.interrupt();
Thread thread2 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().isInterrupted());
}
}
};
thread2.start();
thread2.interrupt();
}
}
public static void main(String[] args) {
//继承的写法1.自定义一个类来继承
MyThread myThread = new
myThread.start();
//继承的写法2.使用一个匿名内部类
Thread t = new Thread(){//属于继承Thread但没有名称的子类
@Override
public void run() {
System.out.println("匿名内部类 run");
}
};
t.start();
}
//继承的方式:1.继承Thread,2.重写run方法(定义要执行的任务代码)
private static class MyThread extends Thread{
@Override
public void run() {
System.out.println("mythread run");
}
}
} public class 实现Runnable {
public static void main(String[] args) {
//写法1
Thread t1 = new Thread(new MyRunnable());
t1.start();
//写法2:匿名内部类
Runnable r2 = new Runnable() {
@Override
public void run() {
System.out.println("匿名内部类run");
}
};
Thread t2 = new Thread(r2);
t2.start();
//写法3:把匿名内部类对象,直接写在构造方法的参数上
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("匿名内部类run");
}
});
t3.start();
//写法4:lambda表达式
Thread t4 = new Thread(() -> System.out.println("匿名内部类run"));
t4.start();
}
private static class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("MyRunnable run");
}
}
} - 设置标志位
public class 自定义标志位 { //先设计一个标志位:表示是否被中断。 private static volatile boolean 是否被中断 = false; public static void main(String[] args) throws InterruptedException { Thread t = new Thread(new Runnable() { @Override public void run() { //循环10次,每次打印次数,并休眠1秒 try { //判断条件加上标志位 for (int i = 0; i < 10 && !是否被中断; i++) { System.out.println(i); //自定义标志位,能够实现某些条件的中断 //但如果线程处于等待/超时等待/阻塞, 就没法中断 //修改时间为100秒就没法中断 Thread.sleep(1000); } } catch (InterruptedException e) { e.printStackTrace(); } } }); t.start(); //要中断t线程:等3后再中断 Thread.sleep(3000); 是否被中断 = true; } } - Thread类内部包含了一个boolean的变量可以用来作为标记位,标记是否被中断Thread.interrupted()或者Thread.currentThread().isInterrupted()都可以
public class demo { public static void main(String[] args) { Thread t = new Thread() { @Override public void run() { while (!Thread.currentThread().isInterrupted()) { System.out.println("thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); break; } } } }; t.start(); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } t.interrupt(); } } - Thread.interrupted()线程中断会清除标记位,Thread.currentThread().isInterrupted()线程标记位不会被清空
public class IsInterrupted { public static void main(String[] args) { Thread thread1 = new Thread(){ @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println(Thread.interrupted()); } } }; thread1.start(); thread1.interrupt(); Thread thread2 = new Thread(){ @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().isInterrupted()); } } }; thread2.start(); thread2.interrupt(); } }
四、线程等待
使用join方法
public class 线路等待 {
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("thread");
try {
Thread.sleep(1000);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
});
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("bit");
}
}
五、线程休眠
使用sleep方法
public class 线程休眠 {
public static void main(String[] args){
Thread t = new Thread() {
@Override
public void run() {
while (true) {
System.out.println("thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
}
}



