zoukankan      html  css  js  c++  java
  • java多线程创建


    package com.mayikt.thread.days01;
    
    /**
     * @author 余胜军
     * @ClassName Thread01
     * @qq 644064779
     * @addres www.mayikt.com
     * 微信:yushengjun644
     */
    public class Thread01 extends Thread {
        /**
         * 线程执行的代码 就是在run方法中  执行完毕 线程死亡
         */
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "<我是子线程>");
            try {
                // 当前线程阻塞3s 时间
                Thread.sleep(3000);
            } catch (Exception e) {
    
            }
            System.out.println(Thread.currentThread().getName() + "<阻塞完毕!>");
        }
    
        public static void main(String[] args) {
            // 启动线程 调用start方法而不是run方法
            System.out.println(Thread.currentThread().getName());
            // 调用start()线程是不是立即被cpu调度执行 就绪状态 ----等待cpu调度 线程从 就绪到运行状态
            new Thread01().start();
            new Thread01().start();
            System.out.println("主线程执行完毕!");
        }
    }

    package com.mayikt.thread.days01;
    
    /**
     * @author 余胜军
     * @ClassName Thread02
     * @qq 644064779
     * @addres www.mayikt.com
     * 微信:yushengjun644
     */
    public class ThreadRunnable implements Runnable {
    
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "<我是子线程>");
        }
    
        public static void main(String[] args) {
    //        // 启动线程
    //        new Thread(new ThreadRunnable()).start();
            // 2.使用匿名内部类的形式创建线程
            new Thread(() -> System.out.println(Thread.currentThread().getName() + "<我是子线程>"), "蚂蚁课堂余胜军线程").start();
    
        }
    }

    package com.mayikt.thread.days01;
    
    import java.util.concurrent.Callable;
    
    /**
     * @author 余胜军
     * @ClassName ThreadCallable
     * @qq 644064779
     * @addres www.mayikt.com
     * 微信:yushengjun644
     */
    public class ThreadCallable implements Callable<Integer> {
        /**
         * 当前线程需要执行的代码 返回结果
         *
         * @return
         * @throws Exception
         */
        @Override
        public Integer call() throws Exception {
            System.out.println(Thread.currentThread().getName() + "开始执行..");
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
    
            }
            System.out.println(Thread.currentThread().getName() + "返回1");
            return 1;
        }
    }
    package com.mayikt.thread.days01;
    
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    import java.util.concurrent.locks.LockSupport;
    
    /**
     * @author 余胜军
     * @ClassName Thread02
     * @qq 644064779
     * @addres www.mayikt.com
     * 微信:yushengjun644
     */
    public class Thread02 {
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            ThreadCallable threadCallable = new ThreadCallable();
            FutureTask<Integer> integerFutureTask = new FutureTask<Integer>(threadCallable);
            new Thread(integerFutureTask).start();
            // 我的主线程需要等待我们子线程给我的返回结果
            Integer result1 = integerFutureTask.get();
            System.out.println(Thread.currentThread().getName() + "," );
    //        LockSupport.park();
    //        LockSupport.unpark();
            // juc 并发中
    //        Thread thread01 = new Thread(new Runnable() {
    //            @Override
    //            public void run() {
    //                System.out.println("我是子线程开始");
    //                LockSupport.park();
    //                System.out.println("我是子线程结束");
    //            }
    //        });
    //        thread01.start();
    //        try {
    //            Thread.sleep(3000);
    //        } catch (Exception e) {
    //
    //        }
    //        LockSupport.unpark(thread01);
    
        }
    }

    线程池

    package com.mayikt.thread.days01;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * @author 余胜军
     * @ClassName Test03
     * @qq 644064779
     * @addres www.mayikt.com
     * 微信:yushengjun644
     */
    public class Test03 {
        public static void main(String[] args) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            executorService.execute(() -> System.out.println(Thread.currentThread().getName() + "我是子线程"));
        }
    }

  • 相关阅读:
    grep 匹配打印的上下几行
    java List<String>的初始化
    HashMap优雅的初始化方式以及引申
    protocol buffer开发指南(官方)
    20种常用的DOS命令小结
    linux xfs文件系统无法用readdir获取dirent文件类型d_type则用stat获取暨stat函数讲解
    java中jar命令打包一个文件夹下的所有文件
    C/C++中的格式化字符
    自定义标签(JspFragment类、invoke方法、开发带属性的标签)
    java中Scanner类nextLine()和next()的区别和使用方法
  • 原文地址:https://www.cnblogs.com/angdh/p/15605922.html
Copyright © 2011-2022 走看看