Skip to content

Commit 67d9f53

Browse files
author
shiqifeng
committed
多线程编程1.3
1 parent 04d3738 commit 67d9f53

File tree

11 files changed

+201
-0
lines changed

11 files changed

+201
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
<version>3.8.1</version>
2020
<scope>test</scope>
2121
</dependency>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<version>RELEASE</version>
26+
<scope>test</scope>
27+
</dependency>
2228
</dependencies>
2329

2430

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by shiqifeng on 2016/12/27.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class ExampleCurrentThread extends Thread{
8+
9+
public ExampleCurrentThread(){
10+
System.out.println("构造方法的打印:" + Thread.currentThread().getName());
11+
}
12+
13+
@Override
14+
public void run() {
15+
super.run();
16+
System.out.println("run方法的打印:" + Thread.currentThread().getName());
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by shiqifeng on 2016/12/27.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class ExampleRunable implements Runnable{
8+
9+
public void run() {
10+
System.out.println("这是实现Runnable接口的类");
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by shiqifeng on 2016/12/27.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class ExampleThread extends Thread{
8+
9+
@Override
10+
public void run() {
11+
super.run();
12+
System.out.println("这是一个继承自Thread的ExampleThread");
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by shiqifeng on 2016/12/27.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class NotShareDataThread extends Thread{
8+
9+
private int count = 5;
10+
11+
12+
public NotShareDataThread(String name){
13+
super();
14+
this.setName(name);
15+
}
16+
17+
@Override
18+
public void run() {
19+
super.run();
20+
System.out.println("++++++++++++++++++");
21+
while (count > 0){
22+
count--;
23+
System.out.println(currentThread().getName() + "计算,count=" +count);
24+
}
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by shiqifeng on 2016/12/27.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class ShareDataThread extends Thread{
8+
9+
private int count = 5;
10+
11+
@Override
12+
public void run() {
13+
super.run();
14+
count--;
15+
System.out.println(currentThread().getName() + "count=" + count);
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.byhieg.threadtutorialtest.char01test;
2+
3+
import cn.byhieg.threadtutorial.char01.ExampleCurrentThread;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by shiqifeng on 2016/12/27.
8+
* Mail byhieg@gmail.com
9+
*/
10+
public class ExampleCurrentThreadTest extends TestCase {
11+
12+
public void testInit() throws Exception{
13+
ExampleCurrentThread thread = new ExampleCurrentThread();
14+
}
15+
16+
public void testRun() throws Exception {
17+
ExampleCurrentThread thread = new ExampleCurrentThread();
18+
thread.start();
19+
20+
Thread.sleep(1000);
21+
22+
}
23+
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.byhieg.threadtutorialtest.char01test;
2+
3+
import cn.byhieg.threadtutorial.char01.ExampleRunable;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by shiqifeng on 2016/12/27.
8+
* Mail byhieg@gmail.com
9+
*/
10+
public class ExampleRunableTest extends TestCase {
11+
public void testRun() throws Exception {
12+
new Thread(new ExampleRunable()).start();
13+
14+
Thread.sleep(1000);
15+
}
16+
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.byhieg.threadtutorialtest.char01test;
2+
3+
import cn.byhieg.threadtutorial.char01.ExampleThread;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by shiqifeng on 2016/12/27.
8+
* Mail byhieg@gmail.com
9+
*/
10+
public class ExampleThreadTest extends TestCase {
11+
12+
public void testRun() throws Exception {
13+
ExampleThread exampleThread = new ExampleThread();
14+
exampleThread.start();
15+
16+
Thread.sleep(1000);
17+
}
18+
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.byhieg.threadtutorialtest.char01test;
2+
3+
import cn.byhieg.threadtutorial.char01.NotShareDataThread;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by shiqifeng on 2016/12/27.
8+
* Mail byhieg@gmail.com
9+
*/
10+
public class NotShareDataThreadTest extends TestCase {
11+
12+
public void testRun() throws Exception {
13+
NotShareDataThread a = new NotShareDataThread("A");
14+
NotShareDataThread b = new NotShareDataThread("B");
15+
NotShareDataThread c = new NotShareDataThread("C");
16+
17+
a.start();
18+
b.start();
19+
c.start();
20+
21+
Thread.sleep(1000);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)