551 . 通过继承Thread类,重写run方法;
662 . 通过实现runable接口;
773 . 通过实现callable接口这三种方式,下面看具体demo。
8-
9- public class CreateThreadDemo {
10-
11- public static void main(String[] args) {
12- //1.继承Thread
13- Thread thread = new Thread() {
14- @Override
15- public void run() {
16- System.out.println("继承Thread");
17- super.run();
18- }
19- };
20- thread.start();
21- //2.实现runable接口
22- Thread thread1 = new Thread(new Runnable() {
23- @Override
24- public void run() {
25- System.out.println("实现runable接口");
26- }
27- });
28- thread1.start();
29- //3.实现callable接口
30- ExecutorService service = Executors.newSingleThreadExecutor();
31- Future<String> future = service.submit(new Callable() {
32- @Override
33- public String call() throws Exception {
34- return "通过实现Callable接口";
35- }
36- });
37- try {
38- String result = future.get();
39- System.out.println(result);
40- } catch (InterruptedException e) {
41- e.printStackTrace();
42- } catch (ExecutionException e) {
43- e.printStackTrace();
44- }
45- }
46-
47- }
48-
8+ ``` Java
9+ public class CreateThreadDemo {
10+ public static void main (String [] args ) {
11+ // 1.继承Thread
12+ Thread thread = new Thread () {
13+ @Override
14+ public void run () {
15+ System . out. println(" 继承Thread" );
16+ super . run();
17+ }
18+ };
19+ thread. start();
20+ // 2.实现runable接口
21+ Thread thread1 = new Thread (new Runnable () {
22+ @Override
23+ public void run () {
24+ System . out. println(" 实现runable接口" );
25+ }
26+ });
27+ thread1. start();
28+ // 3.实现callable接口
29+ ExecutorService service = Executors . newSingleThreadExecutor();
30+ Future<String > future = service. submit(new Callable () {
31+ @Override
32+ public String call () throws Exception {
33+ return " 通过实现Callable接口" ;
34+ }
35+ });
36+ try {
37+ String result = future. get();
38+ System . out. println(result);
39+ } catch (InterruptedException e) {
40+ e. printStackTrace();
41+ } catch (ExecutionException e) {
42+ e. printStackTrace();
43+ }
44+ }
45+ }
46+ ```
4947三种新建线程的方式具体看以上注释,需要主要的是:
5048
5149
@@ -89,9 +87,9 @@ interrupted()对当前线程进行中断操作,该方法会清除中断标
8987
9088
9189下面结合具体的实例来看一看
92-
93- public class InterruptDemo {
94- public static void main(String[] args) throws InterruptedException {
90+ ``` Java
91+ public class InterruptDemo {
92+ public static void main (String [] args ) throws InterruptedException {
9593 // sleepThread睡眠1000ms
9694 final Thread sleepThread = new Thread () {
9795 @Override
@@ -120,7 +118,7 @@ interrupted()对当前线程进行中断操作,该方法会清除中断标
120118 System . out. println(" busyThread isInterrupted: " + busyThread. isInterrupted());
121119 }
122120 }
123-
121+ ```
124122输出结果
125123> sleepThread isInterrupted: false
126124> busyThread isInterrupted: true
@@ -143,7 +141,7 @@ Thread类除了提供join()方法外,另外还提供了超时等待的方法
143141 }
144142
145143可以看出来当前等待对象threadA会一直阻塞,直到被等待对象threadB结束后即isAlive()返回false的时候才会结束while循环,当threadB退出时会调用notifyAll()方法通知所有的等待线程。下面用一个具体的例子来说说join方法的使用:
146-
144+ ``` Java
147145 public class JoinDemo {
148146 public static void main (String [] args ) throws InterruptedException {
149147 for (int i = 0 ; i <= 10 ; i++ ) {
@@ -171,9 +169,9 @@ Thread类除了提供join()方法外,另外还提供了超时等待的方法
171169 }
172170 }
173171}
174-
172+ ```
175173输出结果为:
176-
174+ ```
177175子线程执行完毕
178176主线程执行完毕
179177>~~~~~~~~~~~~~~~
@@ -184,6 +182,7 @@ Thread类除了提供join()方法外,另外还提供了超时等待的方法
184182主线程执行完毕
185183>~~~~~~~~~~~~~~~
186184........
185+ ```
187186
188187在上面的例子中一个创建了10个线程,每个线程都会等待前一个线程结束才会继续运行。可以通俗的理解成接力,前一个线程将接力棒传给下一个线程,然后又传给下一个线程......
189188
0 commit comments