Skip to content

Commit e497d58

Browse files
committed
完成Java多线程到1.8
1 parent 67d9f53 commit e497d58

17 files changed

Lines changed: 379 additions & 0 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by byhieg on 16/12/27.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class AliveOtherThread extends Thread{
8+
9+
public AliveOtherThread(){
10+
System.out.println("AliveOtherThread--------begin");
11+
System.out.println("Thread.currentThread().getName=" + Thread.currentThread().getName());
12+
System.out.println("Thread.currentThread.isAlive()=" + Thread.currentThread().isAlive());
13+
System.out.println("this.getName()=" + this.getName());
14+
System.out.println("this.isAlive()=" + this.isAlive());
15+
System.out.println("AliveOtherThread--------end");
16+
}
17+
18+
19+
@Override
20+
public void run() {
21+
super.run();
22+
System.out.println("run---------begin");
23+
System.out.println("Thread.currentThread().getName=" + Thread.currentThread().getName());
24+
System.out.println("Thread.currentThread().isAlive()=" + Thread.currentThread().isAlive());
25+
System.out.println("this.getName()=" + this.getName());
26+
System.out.println("this.isAlive()=" + this.isAlive());
27+
System.out.println("run---------end");
28+
29+
}
30+
}
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 byhieg on 16/12/27.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class AliveThread extends Thread{
8+
9+
@Override
10+
public void run() {
11+
super.run();
12+
System.out.println("run方法中是否存活" + " " + this.isAlive());
13+
}
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by byhieg on 16/12/27.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class ComplexCurrentThread extends Thread{
8+
9+
public ComplexCurrentThread() {
10+
System.out.println("begin=========");
11+
System.out.println("Thread.currentThread().getName=" + Thread.currentThread().getName());
12+
13+
System.out.println("this.getName()=" + this.getName());
14+
System.out.println("end===========");
15+
}
16+
17+
@Override
18+
public void run() {
19+
super.run();
20+
System.out.println("run begin=======");
21+
System.out.println("Thread.currentThread().getName=" + Thread.currentThread().getName());
22+
System.out.println("this.getName()=" + this.getName());
23+
System.out.println("run end==========");
24+
}
25+
}
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 byhieg on 16/12/27.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class ExampleIdThread extends Thread{
8+
9+
@Override
10+
public void run() {
11+
super.run();
12+
System.out.println(currentThread().getName() + " +++" + currentThread().getId());
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 byhieg on 16/12/27.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class ExampleInterruptThread extends Thread{
8+
9+
@Override
10+
public void run() {
11+
super.run();
12+
try{
13+
for(int i = 0 ; i < 50000000 ; i++){
14+
if (interrupted()){
15+
System.out.println("已经是停止状态,我要退出了");
16+
throw new InterruptedException("停止.......");
17+
}
18+
System.out.println("i=" + (i + 1));
19+
}
20+
}catch (InterruptedException e){
21+
System.out.println("顺利停止");
22+
}
23+
24+
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by byhieg on 16/12/27.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class SleepInterruptThread extends Thread{
8+
9+
@Override
10+
public void run() {
11+
super.run();
12+
try{
13+
System.out.println("run begin");
14+
Thread.sleep(20000000);
15+
System.out.println("run end");
16+
}catch (Exception e){
17+
System.out.println("在沉睡中被停止!进入catch!" + this.isInterrupted());
18+
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by byhieg on 16/12/27.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class SleepThread extends Thread{
8+
9+
@Override
10+
public void run() {
11+
try{
12+
super.run();
13+
System.out.println("run threadName=" + currentThread().getName() + " begin");
14+
Thread.sleep(2000);
15+
System.out.println("run threadName=" + currentThread().getName() + " end");
16+
}catch (Exception e){
17+
e.printStackTrace();
18+
}
19+
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by byhieg on 16/12/28.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class StopLockThread extends Thread{
8+
9+
private SynchronizedObject object;
10+
11+
public StopLockThread(SynchronizedObject object){
12+
this.object = object;
13+
}
14+
15+
16+
@Override
17+
public void run() {
18+
super.run();
19+
object.printString("b","bbb");
20+
}
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.byhieg.threadtutorial.char01;
2+
3+
/**
4+
* Created by byhieg on 16/12/28.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class SynchronizedObject {
8+
private String username = "a";
9+
private String password = "aa";
10+
11+
public String getUsername() {
12+
return username;
13+
}
14+
15+
public void setUsername(String username) {
16+
this.username = username;
17+
}
18+
19+
public String getPassword() {
20+
return password;
21+
}
22+
23+
public void setPassword(String password) {
24+
this.password = password;
25+
}
26+
27+
synchronized public void printString(String username,String password){
28+
try {
29+
this.username = username;
30+
Thread.sleep(1000000);
31+
this.password = password;
32+
}catch (InterruptedException e){
33+
e.printStackTrace();
34+
}
35+
}
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.byhieg.threadtutorialtest.char01test;
2+
3+
import cn.byhieg.threadtutorial.char01.AliveOtherThread;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by byhieg on 16/12/27.
8+
* Mail to byhieg@gmail.com
9+
*/
10+
public class AliveOtherThreadTest extends TestCase {
11+
public void testRun() throws Exception {
12+
AliveOtherThread run = new AliveOtherThread();
13+
Thread thread = new Thread(run);
14+
System.out.println("main begin thread isAlive=" + thread.isAlive());
15+
thread.setName("byhieg");
16+
thread.start();
17+
System.out.println("main end thread isAlive=" + thread.isAlive());
18+
19+
Thread.sleep(3000);
20+
}
21+
22+
}

0 commit comments

Comments
 (0)