标签:nts 继承 方法 target throws init end ext nbsp
1 package com.xiaostudy.thread; 2 3 /** 4 * @desc 第一种开启线程的方式 5 * @author xiaostudy 6 * 7 */ 8 public class Demo1_Thread extends Thread { 9 10 public void run() { 11 for (int i = 0; i < 10; i++) { 12 System.out.println(i + " run()..."); 13 } 14 } 15 16 public static void main(String[] args) { 17 Demo1_Thread demo = new Demo1_Thread(); 18 demo.start(); 19 for (int i = 0; i < 10; i++) { 20 System.out.println(i + " main()..."); 21 } 22 } 23 24 }
上面这里就是当前类的一个线程和main线程一起运行
1 package com.xiaostudy.thread; 2 3 /** 4 * @desc 第二种开启线程的方式 5 * @author xiaostudy 6 * 7 */ 8 public class Demo2_Thread implements Runnable { 9 10 public void run() { 11 for (int i = 0; i < 10; i++) { 12 System.out.println(i + " run()..."); 13 } 14 } 15 16 public static void main(String[] args) { 17 Demo2_Thread demo = new Demo2_Thread(); 18 Thread thread = new Thread(demo); 19 thread.start(); 20 for (int i = 0; i < 10; i++) { 21 System.out.println(i + " main()..."); 22 } 23 } 24 25 }
package com.xiaostudy.thread; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; /** * @desc 第三种开启线程的方式 * @author xiaostudy * */ public class Demo3_Thread implements Callable { public static void main(String[] args) { Callable callable = new Demo3_Thread(); FutureTask futureTask = new FutureTask(callable); Thread thread = new Thread(futureTask); thread.start(); for (int i = 0; i < 10; i++) { System.out.println(i + " main()..."); } } public Object call() throws Exception { for (int i = 0; i < 10; i++) { System.out.println(i + " call()..."); } return null; } }
标签:nts 继承 方法 target throws init end ext nbsp
原文地址:https://www.cnblogs.com/xiaostudy/p/9800601.html