File tree Expand file tree Collapse file tree 10 files changed +246
-0
lines changed
java-se-base-parent/model-multy-thread
src/main/java/com/multy/thread Expand file tree Collapse file tree 10 files changed +246
-0
lines changed Original file line number Diff line number Diff line change 5
5
[ Java基础篇(01):基本数据类型,核心点整理] ( https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484444&idx=2&sn=c5590ea2d71a0a358d2f9cd96c3c2b19&chksm=fdf450a4ca83d9b2383032c1aa354a59a0ca6d52981f2a58d6d3142bde7ed1605b4fcde3b4d9&token=1530600379&lang=zh_CN#rd ) <br />
6
6
[ Java基础篇(02):特殊的String类,和相关扩展API] ( https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484444&idx=1&sn=2cd50529d3d6ff97a9efce6158891c7c&chksm=fdf450a4ca83d9b29f3edf3c4999885278f7e9130a71b1d2f7f54057dcb07a3b95964547e862&token=1530600379&lang=zh_CN#rd ) <br />
7
7
8
+ ### JavaSE并发篇
9
+
10
+ [ Java并发编程(01):线程的创建方式,状态周期管理] ( https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484431&idx=1&sn=9bdac46fa8309a7618542471c8256b8f&chksm=fdf450b7ca83d9a17a3615c5380a163b93b68a7f2c506e533eec4921b87e8386c65dd629a159&token=1530600379&lang=zh_CN#rd ) <br />
11
+
8
12
### JavaEE基础
9
13
10
14
[ JavaEE基础(01):Servlet实现方式,生命周期执行过程] ( https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484304&idx=1&sn=dd6b6852e35031dd07f70d441f3ddc85&chksm=fdf45728ca83de3e158597030cf46b1677eccf533f9e1412690cd64b0ec0cce544711ceabddb&token=1248678182&lang=zh_CN#rd ) <br />
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
3
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4
+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5
+ <parent >
6
+ <artifactId >java-se-base-parent</artifactId >
7
+ <groupId >com.java.se.parent</groupId >
8
+ <version >1.0-SNAPSHOT</version >
9
+ </parent >
10
+ <modelVersion >4.0.0</modelVersion >
11
+ <groupId >com.multy.thread</groupId >
12
+ <artifactId >model-multy-thread</artifactId >
13
+ <!-- 项目构建 -->
14
+ <build >
15
+ <finalName >${project.artifactId} </finalName >
16
+ <resources >
17
+ <resource >
18
+ <directory >src/main/resources</directory >
19
+ <filtering >true</filtering >
20
+ </resource >
21
+ </resources >
22
+ <plugins >
23
+ <plugin >
24
+ <groupId >org.apache.maven.plugins</groupId >
25
+ <artifactId >maven-compiler-plugin</artifactId >
26
+ <version >2.3.2</version >
27
+ <configuration >
28
+ <source >1.8</source >
29
+ <target >1.8</target >
30
+ </configuration >
31
+ </plugin >
32
+ </plugins >
33
+ </build >
34
+ </project >
Original file line number Diff line number Diff line change
1
+ package com .multy .thread ;
2
+
3
+ import java .lang .management .ManagementFactory ;
4
+ import java .lang .management .ThreadInfo ;
5
+ import java .lang .management .ThreadMXBean ;
6
+
7
+ /**
8
+ * 多线程入门案例
9
+ */
10
+ public class HelloThread {
11
+ public static void main (String [] args ) {
12
+ System .out .println ("Hello,Thread" );
13
+ // 当前线程名称
14
+ System .out .println (Thread .currentThread ().getName ());
15
+ // 线程系统的管理接口
16
+ ThreadMXBean threadMXBean = ManagementFactory .getThreadMXBean ();
17
+ long [] threadIds = threadMXBean .getAllThreadIds () ;
18
+ for (long id : threadIds ) {
19
+ ThreadInfo threadInfo = threadMXBean .getThreadInfo (id ) ;
20
+ System .out .println (threadInfo .getThreadId ()+
21
+ ":" +threadInfo .getThreadName ());
22
+ }
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ package com .multy .thread .block01create ;
2
+ /**
3
+ * 线程创建方式一:继承Thread类
4
+ */
5
+ public class CreateThread01 {
6
+ public static void main (String [] args ) {
7
+ // 调用方法
8
+ MyThread1 myThread1 = new MyThread1 () ;
9
+ myThread1 .start ();
10
+ }
11
+ }
12
+ class MyThread1 extends Thread {
13
+ // 设置线程名称
14
+ public MyThread1 (){
15
+ super ("CicadaThread" );
16
+ }
17
+ @ Override
18
+ public void run () {
19
+ System .out .println (Thread .currentThread ().getName ());
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package com .multy .thread .block01create ;
2
+ /**
3
+ * 线程创建方式二:实现 Runnable 接口
4
+ */
5
+ public class CreateThread02 {
6
+ public static void main (String [] args ) {
7
+ Thread thread = new Thread (new MyThread2 (),"MyThread2" ) ;
8
+ thread .start ();
9
+ }
10
+ }
11
+ class MyThread2 implements Runnable {
12
+ @ Override
13
+ public void run () {
14
+ System .out .println (Thread .currentThread ().getName ()+" run ..." );
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ package com .multy .thread .block01create ;
2
+ /**
3
+ * 线程创建方式三:匿名内部类
4
+ */
5
+ public class CreateThread03 {
6
+ public static void main (String [] args ) {
7
+ //方式1
8
+ new Thread ("ThreadName1" ) {
9
+ public void run () {
10
+ System .out .println ("1:" +Thread .currentThread ().getName ());
11
+ };
12
+ }.start ();
13
+
14
+ //方式2
15
+ new Thread (new Runnable () {
16
+ public void run () {
17
+ System .out .println ("2:" +Thread .currentThread ().getName ());
18
+ }
19
+ },"ThreadName2" ){
20
+ // 这里重写了run方法
21
+ @ Override
22
+ public void run () {
23
+ System .out .println ("3:" +Thread .currentThread ().getName ());
24
+ }
25
+ }.start ();
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ package com .multy .thread .block01create ;
2
+
3
+ import java .util .concurrent .Callable ;
4
+ import java .util .concurrent .FutureTask ;
5
+ import java .util .concurrent .TimeUnit ;
6
+
7
+ /**
8
+ * 线程创建方式四:返回值线程
9
+ */
10
+ public class CreateThread04 {
11
+ public static void main (String [] args ) throws Exception {
12
+ MyThread4 myThread4 = new MyThread4 ();
13
+ FutureTask <Integer > task = new FutureTask <>(myThread4 );
14
+ Thread thread = new Thread (task ,"TaskThread" );
15
+ thread .start ();
16
+ // 等待获取结果
17
+ // Integer result = task.get();
18
+ // 设置获取结果的等待时间,超时抛出:TimeoutException
19
+ Integer result = task .get (3 , TimeUnit .SECONDS ) ;
20
+ System .out .println ("result=" +result );
21
+ }
22
+ }
23
+ class MyThread4 implements Callable <Integer > {
24
+ // 封装线程执行的任务
25
+ @ Override
26
+ public Integer call () throws Exception {
27
+ System .out .println (Thread .currentThread ().getName ());
28
+ Thread .sleep (1000 );
29
+ return 2 +3 ;
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ package com .multy .thread .block01create ;
2
+
3
+ import java .util .Timer ;
4
+ import java .util .TimerTask ;
5
+
6
+ /**
7
+ * 线程创建方式五:定时任务
8
+ */
9
+ public class CreateThread05 {
10
+ public static void main (String [] args ) {
11
+ Timer timer = new Timer ();
12
+ timer .schedule (new TimerTask () {
13
+ @ Override
14
+ public void run () {
15
+ System .out .println ("延迟1s,每隔3s执行一次" );
16
+ }
17
+ }, 1000 , 3000 );
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ package com .multy .thread .block01create ;
2
+
3
+ import java .util .concurrent .Executor ;
4
+ import java .util .concurrent .Executors ;
5
+
6
+ /**
7
+ * 线程创建方式六:线程池
8
+ */
9
+ public class CreateThread06 {
10
+ public static void main (String [] args ) {
11
+ Executor threadPool = Executors .newFixedThreadPool (5 );
12
+ for (int i = 0 ;i < 5 ; i ++) {
13
+ threadPool .execute (new Runnable () {
14
+ @ Override
15
+ public void run () {
16
+ System .out .println (Thread .currentThread ().getName ());
17
+ }
18
+ });
19
+ }
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package com .multy .thread .block01create ;
2
+
3
+ import java .util .concurrent .Callable ;
4
+ import java .util .concurrent .FutureTask ;
5
+ import java .util .concurrent .TimeUnit ;
6
+
7
+ /**
8
+ * 线程状态和周期
9
+ */
10
+ public class StateCycle01 {
11
+ public static void main (String [] args ) throws Exception {
12
+ // 进入初始状态
13
+ StateThread01 stateThread01 = new StateThread01 ();
14
+ FutureTask <String > task = new FutureTask <>(stateThread01 );
15
+ Thread thread = new Thread (task ,"GetValueThread" );
16
+ // 运行状态
17
+ thread .start ();
18
+ // 超时等待结果
19
+ String result = task .get (3 , TimeUnit .SECONDS ) ;
20
+ System .out .println ("result=" +result );
21
+
22
+ StateThread02 stateThread02 = new StateThread02 () ;
23
+ Thread thread1 = new Thread (stateThread02 ,"WaitThread" );
24
+ thread1 .start ();
25
+ }
26
+ }
27
+ class StateThread01 implements Callable <String > {
28
+ @ Override
29
+ public String call () throws Exception {
30
+ // 超时等待
31
+ Thread .sleep (1000 );
32
+ return "Hello,Cicada" ;
33
+ }
34
+ }
35
+ class StateThread02 implements Runnable {
36
+ @ Override
37
+ public void run () {
38
+ synchronized (StateCycle01 .class ) {
39
+ System .out .println ("进入线程..." );
40
+ try {
41
+ // 等待状态,放弃对象锁
42
+ StateCycle01 .class .wait (2000 );
43
+ } catch (Exception e ) {
44
+ e .printStackTrace ();
45
+ }
46
+ System .out .println ("线程继续..." );
47
+ }
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments