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() + "我是子线程")); } }