File tree Expand file tree Collapse file tree
thread/src/main/java/com/thread/demo1 Expand file tree Collapse file tree 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 >example</artifactId >
7+ <groupId >com.fumj</groupId >
8+ <version >0.0.1-SNAPSHOT</version >
9+ </parent >
10+ <modelVersion >4.0.0</modelVersion >
11+
12+ <artifactId >collectiondemo</artifactId >
13+
14+ <properties >
15+ <maven .compiler.source>8</maven .compiler.source>
16+ <maven .compiler.target>8</maven .compiler.target>
17+ </properties >
18+
19+ </project >
Original file line number Diff line number Diff line change 1+ package demo1 ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ /**
7+ * <p>
8+ * TODO
9+ * </p>
10+ *
11+ * @author fumj
12+ * @version 1.0
13+ * @date 2022/4/28 16:54
14+ */
15+ public class Demo1 {
16+ public static void main (String [] args ) {
17+ List <String > list = new ArrayList <>(16 );
18+ }
19+ }
Original file line number Diff line number Diff line change 1717 <module >kinfe4j-demo</module >
1818 <module >jvm</module >
1919 <module >http-connect-pool-demo</module >
20+ <module >stringtemplatedemo</module >
21+ <module >collectiondemo</module >
2022 </modules >
2123 <parent >
2224 <groupId >org.springframework.boot</groupId >
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 >example</artifactId >
7+ <groupId >com.fumj</groupId >
8+ <version >0.0.1-SNAPSHOT</version >
9+ </parent >
10+ <modelVersion >4.0.0</modelVersion >
11+
12+ <artifactId >stringtemplatedemo</artifactId >
13+
14+ <properties >
15+ <maven .compiler.source>8</maven .compiler.source>
16+ <maven .compiler.target>8</maven .compiler.target>
17+ </properties >
18+
19+ <dependencies >
20+ <dependency >
21+ <groupId >org.antlr</groupId >
22+ <artifactId >ST4</artifactId >
23+ <version >4.3.2</version >
24+ <scope >compile</scope >
25+ </dependency >
26+ </dependencies >
27+
28+ </project >
Original file line number Diff line number Diff line change 1+ package demo ;
2+
3+ import org .stringtemplate .v4 .ST ;
4+
5+ /**
6+ * <p>
7+ * TODO
8+ * </p>
9+ *
10+ * @author fumj
11+ * @version 1.0
12+ * @date 2022/4/7 17:44
13+ */
14+ public class Demo1 {
15+ public static void main (String [] args ) {
16+ ST hello = new ST ("Hello,<name>" );
17+ hello .add ("name" ,"fumj" );
18+ System .out .println (hello .render ());
19+
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package com .thread .demo1 ;
2+
3+ import java .util .concurrent .BlockingQueue ;
4+
5+ /**
6+ * <p>
7+ * TODO
8+ * </p>
9+ *
10+ * @author fumj
11+ * @version 1.0
12+ * @date 2022/4/27 14:55
13+ */
14+ public class Consumer {
15+ BlockingQueue storage ;
16+
17+ public Consumer (BlockingQueue storage ) {
18+ this .storage = storage ;
19+ }
20+
21+ public boolean needMoreNums (){
22+ if (Math .random () > 0.9 ){
23+ return false ;
24+ }
25+
26+ return true ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ package com .thread .demo1 ;
2+
3+ import java .util .concurrent .BlockingQueue ;
4+
5+ /**
6+ * <p>
7+ * TODO
8+ * </p>
9+ *
10+ * @author fumj
11+ * @version 1.0
12+ * @date 2022/4/27 14:48
13+ */
14+ public class StopThread implements Runnable {
15+
16+ public volatile boolean canceled = false ;
17+
18+ BlockingQueue storage ;
19+
20+ public StopThread (BlockingQueue storage ) {
21+ this .storage = storage ;
22+ }
23+
24+ @ Override
25+ public void run () {
26+ int num = 0 ;
27+ try {
28+
29+ while (!canceled && num < 1000 ){
30+ if (num % 10 == 0 ){
31+ storage .put (num );
32+ System .out .println (num + "是10的倍数" );
33+ }
34+ num ++;
35+ Thread .sleep (1 );
36+ }
37+ } catch (InterruptedException e ) {
38+ e .printStackTrace ();
39+ }finally {
40+ System .out .println ("生产者结束" );
41+ }
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ package com .thread .demo1 ;
2+
3+ import com .sun .deploy .uitoolkit .impl .awt .AWTDragHelper ;
4+ import oracle .jvm .hotspot .jfr .Producer ;
5+
6+ import java .util .concurrent .ArrayBlockingQueue ;
7+
8+ /**
9+ * <p>
10+ * TODO
11+ * </p>
12+ *
13+ * @author fumj
14+ * @version 1.0
15+ * @date 2022/4/27 14:57
16+ */
17+ public class ThreadTest1 {
18+ public static void main (String [] args ) throws Exception {
19+ ArrayBlockingQueue storage = new ArrayBlockingQueue (8 );
20+ StopThread producer = new StopThread (storage );
21+ Consumer consumer = new Consumer (storage );
22+
23+ Thread producerThread = new Thread (producer );
24+ producerThread .start ();
25+ Thread .sleep (500 );
26+
27+ while (consumer .needMoreNums ()){
28+ System .out .println (consumer .storage .take () + "被消费了" );
29+ Thread .sleep (100 );
30+ }
31+
32+ System .out .println ("消费者不需要更多数据了。" );
33+ producer .canceled = true ;
34+
35+ System .out .println (producer .canceled );
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments