Java多线程的几种常见写法

多线程的几种写法

1)继承Thread

class MyThread extends Thread{
    @Override
    public void run(){
        while(true){
        System.out.println("hello Thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
public class Demo1 {
    public static void main(String[] args) {
        Thread t = new MyThread();
        t.start();
        while (true) {
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

2)通过实现Runnable接口来实现

package Thread;
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 {
    public static void main(String[] args) {
        Thread t=new Thread(new MyRunnable());
        t.start();
        while(true){
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

3)匿名内部类

package Thread;

public class Demo3 {
    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) {
                        throw new RuntimeException(e);
                    }
                }
            }

        };
        t.start();
        while(true){
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

4)匿名内部类,针对Runnable

package Thread;

public class Demo4 {
    public static void main(String[] args) {
        Thread t=new Thread(new MyRunnable(){
            @Override
            public void run(){
                while(true){
                System.out.println("Thread");
            }
            }


        });
        t.start();
        while(true){
        System.out.println("main");
    }
    }
}

5)使用lambda表达式,lambda本质上就是针对匿名内部类的平替

package Thread;

import org.w3c.dom.ls.LSOutput;

public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        });

        t.start();
        Thread.sleep(500);
        System.out.println(t.isAlive());

    }
}

上述的5种写法,本质上都是

1)要把线程执行的任务内容表示出来

2)通过Thread的start来创建/启动系统中的线程(Thread对象和操作系统中的线程是一一对象的关系)

catch语句中的代码

这个throw是继续再抛出新的异常

 try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

这个是打印异常调用栈


            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

2)再main方法中处理sleep有两种选择

1,throws

2,try catch

在线程的run中就只有一个选择了只能try catch,因为重写的时候,就要求方法的签名得是一样的

是否后台线程isDaemon()

关于线程各种属性的设置,都要放到start之前,一旦线程已经启动,开弓没有回头箭,再设置就来不及了

package Thread;

import org.w3c.dom.ls.LSOutput;

public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        });
t.isDaemon();
        t.start();
        Thread.sleep(500);
       

    }
}

是否存活isAlive()

package Thread;

import org.w3c.dom.ls.LSOutput;

public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        });

        t.start();
        Thread.sleep(4000);
        System.out.println(t.isAlive());

    }
}

是否中断isInterrupted()

1)第一种纯自己实现

需要让需要终止的线程的入口方法尽快执行结束(跳出循环,还是尽快return 都无所谓)

package Thread;

public class Demo6 {
    private static Boolean isRunning =true;
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread(() ->{
            while(isRunning){

                System.out.println("Thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("线程结束了");
        });
        t.start();
        System.out.println("main");

        Thread.sleep(5000);
        isRunning=false;
        System.out.println("变量结束");
    }
}

2)第二种

使用Thread提供的interrupt方法和isInterruptted方法来实现上述的效果,实际上Thread里面内置了一个,使用内置的标志位,功能要更强大

package Thread;

public class Demo7 {
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread(() ->{
           while(!Thread.currentThread().isInterrupted()){
               System.out.println("Thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });

        t.start();
        t.interrupt();
        Thread.sleep(5000);
        System.out.println("结束进程");

    }
}

 当使用了Interrupt方法之后,此时,要不要结束,都是t线程自己决定的!!!

如果代码没有sleep,确实是直接修改了标志位就完了,有sleep,并且是触发Interrupt的时候,线程正在sleep,sleep被唤醒的同时,就会清楚刚才的标志位(又改回false)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值