1
+ package com .wcan .thread .execrise ;
2
+
3
+ import java .util .Scanner ;
4
+
5
+ public class PrintNumberExecrise {
6
+
7
+ public static void main (String [] args ) {
8
+ PrintNumberThread numberThread = new PrintNumberThread ();
9
+ InputWordThread inputWordThread = new InputWordThread (numberThread );
10
+ numberThread .start ();
11
+ inputWordThread .start ();
12
+
13
+ }
14
+ }
15
+
16
+ /**
17
+ *
18
+ * @Title: PrintNumberExecrise.java
19
+ * @Package: com.wcan.thread.execrise
20
+ * @Description: 打印100以内的随机数
21
+ * @author: wcan
22
+ * @date: 2020年1月9日 上午12:18:17
23
+ * @version: V1.0
24
+ */
25
+ class PrintNumberThread extends Thread {
26
+
27
+ private boolean flag ;
28
+
29
+ public void setFlag (boolean flag ) {
30
+ this .flag = flag ;
31
+ }
32
+
33
+ @ Override
34
+ public void run () {
35
+ while (true ) {
36
+ try {
37
+ Thread .sleep (300 );
38
+ } catch (InterruptedException e ) {
39
+ // TODO Auto-generated catch block
40
+ e .printStackTrace ();
41
+ }
42
+ int num = (int )(Math .random ()*100 );
43
+ System .out .println (num );
44
+ }
45
+ }
46
+
47
+ }
48
+
49
+ /**
50
+ *
51
+ * @Title: PrintNumberExecrise.java
52
+ * @Package: com.wcan.thread.execrise
53
+ * @Description: 从键盘上读取字符
54
+ * @author: wcan
55
+ * @date: 2020年1月9日 上午12:18:56
56
+ * @version: V1.0
57
+ */
58
+ class InputWordThread extends Thread {
59
+ Scanner in = new Scanner (System .in );
60
+ PrintNumberThread pnt ;
61
+ public InputWordThread (PrintNumberThread pnt ) {
62
+ // TODO Auto-generated constructor stub
63
+ this .pnt = pnt ;
64
+ }
65
+ @ Override
66
+ public void run () {
67
+ while (true ) {
68
+ System .out .println ("请输入:" );
69
+ char at = in .next ().toUpperCase ().charAt (0 );
70
+ if (at == 'Q' ) {
71
+ pnt .setFlag (false );
72
+ break ;
73
+ }
74
+ }
75
+ }
76
+ }
0 commit comments