hello,大家好,好久不见,甚是想念,今天为大家带来Thread类的相关知识点
🐷线程创建
🐷线程中断
🐷线程等待
🐷线程休眠
🐷获取线程实例
线程创建
线程创建有五种方式
1.继承Thread方法,重写run方法
2.实现runnable方法,重写run
3.继承Thread,使用匿名内部类
4.实现Runnable,使用匿名内部类
5.使用lambda表达式
这5个里面用的最多的还是第五个,下面我们一个一个来看
🎉继承Thread方法,重写run方法
package threading;
/**
* Created with IntelliJ IDEA.
* Description:演示多线程的基本创建方式
* User: WHY
* Date: 2022-09-02
* Time: 9:46
*/
class MyThread extends Thread {//因为Thread这个类是标准库中的类,而自己写的MyThread 类继承了父类,所以要重写这个
@Override
public void run() {//重写run方法。run方法是thread父类里面已有的方法,此时重写他
while (true) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);//因为run方法重写了,所以在抛异常的时候不能选择用throws,只能try catch;
}
}
}
}
public class Demo1 {
public static void main(String[] args) {
MyThread t=new MyThread();//实例化的时候其实并没有启动线程
t.start();//启动了这个线程
while(true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
2.实现runnable方法,重写run
package threading;
/**
* Created with IntelliJ IDEA.
* Description:
* User: WHY
* Date: 2022-09-03
* Time: 21:38
*/
class MyRunnable implements Runnable{
@Override
public void run() {
while (true) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class demo2 {
//创建线程不仅可以用继承Thread,还有一种方法是实现Runnable接口,重写run
public static void main(String[] args) {
MyRunnable runnable=new MyRunnable();
Thread t=new Thread(runnable);
t.start();
while(true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
3.继承Thread,使用匿名内部类
public class demo1 {
public static void main(String[] args) {
System.out.println("hello main");
new Thread(){
@Override
public void run() {
System.out.println("hello thread");
for(int i = 0;i<4;i++){
System.out.println("hello ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("子线程运行结束");
}
}.start();
for(int i = 0;i<4;i++){
System.out.println("Main线程");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程已经结束了");
}
}
4.
实现Runnable,使用匿名内部类
public class demo {
public static void main(String[] args) {
// 创建一个线程,参数是实现了Runnable接口的匿名类的实例
Thread thread = new Thread(new Runnable() {
public void run() { //实现run() 方法
System.out.println("hello thread");
}
});
thread.start(); // 启动线程
}
}
5.5.使用lambda表达式
public class demo10 {
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.printStackTrace();
}
}
System.out.println("线程执行完了");
},"这是我的线程");
t.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("设置让t线程结束");
}
}
其中lambda表达式是最常用的那个
线程中断
package threading;
/**
* Created with IntelliJ IDEA.
* Description:
* User: WHY
* Date: 2022-09-11
* Time: 14:43
*/
public class demo10 {
public 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.printStackTrace();
}
}
System.out.println("线程执行完了");
},"这是我的线程");
t.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
isQuit=true;
System.out.println("设置让t线程结束");
}
}
package threading;
/**
* Created with IntelliJ IDEA.
* Description:
* User: WHY
* Date: 2022-09-11
* Time: 14:57
*/
public class demo11 {
public static void main(String[] args) {
Thread t=new Thread(()->{
while(!Thread.currentThread().isInterrupted()){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//e.printStackTrace();
break;
}
}
System.out.println("线程执行完了");
},"这是我的线程");
t.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
t.interrupt();//主线程通过这个中断线程,设置标志位为true
System.out.println("设置让t 结束");
}
}
线程等待
public class demo12 {
public static void main(String[] args) {
Thread t=new Thread(()->{
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
},"这是我的代码");
t.start();
System.out.println("join之前");
try {
t.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("join之后");
}
}
线程休眠
这个很简单没啥好说的,就是单纯地Thread.sleep();但是要抛出一个异常
线程创建一个实例
class Counter{
public int count;
public synchronized void increase(){
count++;
}
}
public class demo14 {
private static Counter counter=new Counter();
public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(()->{
for(int i=0;i<50000;i++){
counter.increase();
}
});
Thread t2=new Thread(()->{
for(int i=0;i<50000;i++){
counter.increase();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("counter:"+counter.count);
}
}
使用lambda表达式来创建一个t1,t2实例
public class demo10 {
public 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.printStackTrace();
}
}
System.out.println("线程执行完了");
},"这是我的线程");
t.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
isQuit=true;
System.out.println("设置让t线程结束");
}
}
如上图代码,创建了t实例
今天的讲解就到此为止,我们下期再见,886!!!