(多线程)创建线程的6种方法

目录

🍎继承Thread类

运行结果

🍎继承Thread类的内部类写法

 🍇运行结果

🍎实现Runnable接口

  🍇运行结果

🍎 实现Runnable接口的内部类写法

 🍇运行结果

🍎 lambda写法

 🍇运行结果

🍎callable接口

 🍇运行结果

🥦 Thread类的基本api

🍂start

🍂run

🍂join

🍦运行结果

🍂sleep

🍂 getState


继承Thread类

//继承Thread类
class MyThread1 extends Thread{
    @Override
    public void run() {
        while (true) {
            try {
                System.out.println("继承Thread类");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
public class ThreadDemo1 {
    public static void main(String[] args) {
        Thread t = new MyThread1();
        t.start();
       while (true){
           System.out.println("main线程");
           try {
               Thread.sleep(1000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
    }

}

运行结果

继承Thread类的内部类写法

//继承thread类的内部类写法

public class ThreadDemo2 {
    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();
        while (true){
            System.out.println("main线程");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 运行结果

实现Runnable接口

class MyThread3 implements Runnable{

    @Override
    public void run() {
        while (true){
            System.out.println("实现runnable接口");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class ThreadDemo3 {
    public static void main(String[] args) {
        MyThread3 myThread3 = new MyThread3();
        Thread t = new Thread(myThread3);
        t.start();
        while (true){
            System.out.println("main线程");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

 运行结果

 实现Runnable接口的内部类写法

public class ThreadDemo4 {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("实现runnable接口内部类");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
        while (true){
            System.out.println("main线程");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果

 lambda写法

public class ThreadDemo5 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
            while (true){
                System.out.println("lambda写法");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        while (true){
            System.out.println("main线程");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果

callable接口

public class ThreadDemo6 {
    public static void main(String[] args) {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("callable 写法,有返回值");
                Thread.sleep(1000);
                return 666;
            }
        };
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();
        int re = 0;
        try {
             re = futureTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        while (true){
            System.out.println("main方法  re = "+ re);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果

 Thread类的基本api

start

真正从系统这里创建了一个线程,创建出来的线程启动时候就会自动调用执行自己的run方法。

       如果只是同过Thread类创建一个线程而没有start,运行时仍然只执行main方法,也就是主线程的方法操作。

run

        表示一个线程的入口方法。(我们可以简单理解为run方法为创建出来的线程的main方法。)他会在线程创建并执行时候自动调运,不用我们手动调用,如果我们手动调用run方法,那就只是一个普通的方法在main线程里被调用了,而不是在创建的线程里被调用执行。 

join

线程等待。可以明确规定线程的执行顺序

在哪个线程里调用  t.join  哪个线程就会阻塞等待 t 线程执行结束后再执行。

比如说:现在有三个线程,一个main线程,一个 t1线程,一个 t2线程  。现在在main方法里调用                  t1.join 就会使得main阻塞等待 t1 线程执行结束后再执行,而 t1 线程和 t2 线程仍然时                并发执行。

public class ThreadDemo7 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            while (true){
                System.out.println("这是t1线程");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(()->{
           while(true){
               System.out.println("这是t2线程");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        t1.start();
        t2.start();
        //预计t1,t2并发执行,main方法在t1,t2执行完后再执行
        //但是t1,t2是在死循环种,所以main方法不会打印到
        t1.join();
        t2.join();
        while (true){
            System.out.println("这是main方法");
            Thread.sleep(1000);
        }
    }
}

运行结果

sleep

休眠, Thread.sleep( ) 。括号中可以填休眠的时间,单位毫秒。

需要抛出InterruptdeException异常

 getState

t.getState() 。获取线程当前的状态

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值