zoukankan      html  css  js  c++  java
  • [JAVA]JAVA实现多线程的三种方式

    1、继承Thread类,通过start()方法调用

    public class MultiThreadByExtends extends Thread {
        @Override
        public void run() {
            print("听歌");
        }
        public static void print(String threadName){
            while (true) {
                System.out.println("线程:"+threadName+" 输出。。。");
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
    
        public static void main(String[] args) {
            MultiThreadByExtends t1 = new MultiThreadByExtends();
            t1.start();
            print("写代码");
        }
    }

    输出结果:

    
    

    线程:写代码 输出。。。
    线程:听歌 输出。。。
    线程:写代码 输出。。。
    线程:听歌 输出。。。
    线程:写代码 输出。。。
    线程:听歌 输出。。。
    线程:听歌 输出。。。

     

    由于java单继承的特点,这种方式局限性很大,不能继承其他父类

    2、实现Runable接口

    public class MultiThreadByRunnable implements Runnable {
        @Override
        public void run() {
            print("听歌");
        }
        public static void print(String threadName){
            while (true) {
                System.out.println("线程:"+threadName+" 输出。。。");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
    
        public static void main(String[] args) {
            MultiThreadByRunnable tr1 = new MultiThreadByRunnable();
            //代理方式启动
            Thread thread = new Thread(tr1);
            thread.start();
            print("写代码");
        }
    }

    输出结果:

    线程:写代码 输出。。。
    线程:听歌 输出。。。
    线程:写代码 输出。。。
    线程:听歌 输出。。。
    线程:听歌 输出。。。
    线程:写代码 输出。。。
    线程:听歌 输出。。。
    线程:写代码 输出。。。

    3、实现callable接口

    import java.util.concurrent.*;
    
    public class CallableTest implements Callable<Boolean> {
        @Override
        public Boolean call() throws Exception {
            int count = 0;
            while (true){
                System.out.println("1111");
                Thread.sleep(1000);
                count++;
                if(count >10) {
                    break;
                }
            }
            return true;
        }
    
        public static void main(String[] args) throws ExecutionException, InterruptedException {
    
            CallableTest c = new CallableTest();
            ExecutorService es = Executors.newFixedThreadPool(1);
            Future<Boolean> res = es.submit(c);
            System.out.println(res.get());
            es.shutdownNow();
        }
    }

    该方式的优点是可以得到线程的返回值 

    平时开发中常用到的就是1、2两种方式,至于第三种,属于JUC里面的东西,如果在面试中能答上来,对初级或中级java面试来说绝对是加分项

  • 相关阅读:
    iOS Xcode工程目录的 folder 和 group的区别(蓝色和黄色文件夹的区别)
    携程App的网络性能优化实践
    iOS: NSObject中执行Selector的相关方法
    去空格 whitespaceAndNewlineCharacterSet
    iOS UIButton 设置图片文字垂直排列
    程序启动的完整过程
    iOS:友盟SDK第三方登录 分享及友盟统计的使用
    ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
    判断uiscrollView滑到底部
    ios模拟器键盘不弹出
  • 原文地址:https://www.cnblogs.com/zhengxl5566/p/10332272.html
Copyright © 2011-2022 走看看