目录
Thread类的基本使用
一.线程的创建
线程的创建方法有五类。
1.继承Thread类创建线程
public class MyThread extends Thread{
@Override
public void run() {
while(true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
}
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
while(true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
}
}
2.实现Runnable接口创建线程
public class MyRunable implements Runnable{
@Override
public void run() {
while(true){
System.out.println("hello runnable");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
}
public static void main(String[] args) {
Thread t = new Thread(new MyRunable());
t.start();
while(true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
}
}
3.继承Thread类使用匿名内部类创建线程
public class demo3 extends Thread{
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run() {
while(true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
}
};
t.start();
}
}
4.实现Runnable接口使用匿名内部类创建线程
public class demo4{
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while(true){
System.out.println("hello runnable");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
}
});
t.start();
}
}
5.使用Lambda表达式创建线程
public class Lambda {
public static void main(String[] args) {
Thread t = new Thread(()->{
while (true){
System.out.println("hello Lambda");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
});
t.start();
}
}
二.线程的中断
我们都知道什么时候线程断了,那就是run方法执行完了,有没有什么方法可以让线程提前中断呢?这个时候我们可以自己设置一个标志位,控制标志位从而控制线程什么时候中断。
public class demo5 {
private static boolean isQuit = false;
public static void main(String[] args) {
Thread t = new Thread(()->{
while(!isQuit){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
});
t.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.getStackTrace();
}
//将isQuit改为true则t线程里的while循环就进不去从而结束线程
isQuit = true;
System.out.println("终止t线程");
}
}
其实在Thread里有自带的标志位interrupt,所以我们就可以这样使用
public class demo6 {
public static void main(String[] args) {
Thread t = new Thread(()->{
while(!Thread.currentThread().isInterrupted()){
//Thread.interruted()这是一个静态方法 Thread.currentThread().isInterrupted()这是一个实例方法,
// 其中currentThread能够获取到当前线程的实例,而isInterrupted()就是判断标志位的
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.getStackTrace();
System.out.println("这是收尾工作");
break;
}
}
});
t.start();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.getStackTrace();
}
t.interrupt();
}
}
三.线程的等待
由于线程的随机调度,我们时常会看到两个线程一下你先运行一下我先运行的情况
而我们讨厌随机性,所以有没有什么办法可以让我们控制线程之间的调度呢?
有的那就是join方法,他可以控制线程结束的顺序。
如果我们在main线程里使用了t.join(),那表达的就是等到t线程结束才开始main线程,期间main线程进行堵塞(不参与cpu的调度)。
public class demo7 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(()->{
for (int i = 0; i < 10; i++) {
System.out.println("hello thread");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
});
t.start();
t.join();
for (int i = 0; i < 10; i++) {
System.out.println("hello main");
Thread.sleep(500);
}
}
}
我们也可以在join里设置一个时间,表示等到这个时间我就不等了
public class demo7 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(()->{
for (int i = 0; i < 10; i++) {
System.out.println("hello thread");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
});
t.start();
t.join(3000);
for (int i = 0; i < 10; i++) {
System.out.println("hello main");
Thread.sleep(500);
}
}
}
四.线程的休眠
线程的休眠sleep说的简单点就是,有两个队列一个就绪队列,一个阻塞队列,若一个线程调用了sleep的话则进入阻塞队列,等到阻塞的时间到了,则进入就绪队列等待cpu的调度(而不是立刻执行)。
五.获取线程实例
public static Thread currentThread();
获取当前这个线程对应的Thread对象的引用,那个线程调用这个方法得到的就是那个线程的引用