Skip to content

Commit 42cfd13

Browse files
thread
1 parent 221e299 commit 42cfd13

5 files changed

Lines changed: 117 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
*/.idea/
77
*.docs
88
*.doc
9-
src/
9+
/src/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Test {
2+
public static void main(String[] args) {
3+
//获取当前线程
4+
Thread thread = Thread.currentThread() ;
5+
//获取线程名字
6+
System.out.println(thread.getName());
7+
//设置线程名字
8+
thread.setName("hello");
9+
10+
System.out.println(thread.getName());
11+
}
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
public class ThreadDemo01 extends Thread{
3+
4+
//线程的执行方法
5+
@Override
6+
public void run() {
7+
for(int i=0;i<100;i++){
8+
System.out.println( Thread.currentThread().getName()+":"+i );
9+
}
10+
}
11+
12+
public static void main(String[] args) {
13+
ThreadDemo01 t1 = new ThreadDemo01();
14+
t1.setName("t1");
15+
ThreadDemo01 t2 = new ThreadDemo01();
16+
t2.setName("t2");
17+
18+
t1.start();
19+
System.out.println("--------");
20+
t2.start();
21+
}
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class ThreadDemo02 implements Runnable {
2+
3+
//线程执行的方法
4+
@Override
5+
public void run() {
6+
for(int i=0;i<100;i++){
7+
try {
8+
Thread.sleep(3000);
9+
} catch (InterruptedException e) {
10+
e.printStackTrace();
11+
}
12+
13+
System.out.println( Thread.currentThread().getName()+":"+i );
14+
}
15+
}
16+
17+
//Runnable方式仍然是借用Thread来实现的多线程
18+
public static void main(String[] args) {
19+
ThreadDemo02 t1 = new ThreadDemo02();
20+
ThreadDemo02 t2 = new ThreadDemo02();
21+
22+
23+
//启动线程需要使用start()方法,但是Runnable没有提供start(),如何解决?
24+
//Thread有start() Runnable没有start()
25+
//Runnable ->Thread
26+
//Thread(Runable )
27+
Thread th1 = new Thread( t1,"T1-");
28+
Thread th2 = new Thread( t2);
29+
// th1.setName("T1");
30+
31+
th1.setPriority(Thread.MAX_PRIORITY);
32+
th2.setPriority(Thread.MIN_PRIORITY);
33+
34+
th2.setName("T2");
35+
th1.start();
36+
37+
th2.start();
38+
39+
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class ThreadDemo03 implements Runnable {
2+
3+
//Th1线程执行的方法
4+
@Override
5+
public void run() {
6+
for(int i=0;i<10;i++){
7+
System.out.println( Thread.currentThread().getName()+":"+i );
8+
if(i == 3){
9+
Thread.yield();
10+
System.out.println("礼让....");
11+
}
12+
}
13+
}
14+
15+
//线程th1:执行“ T1:数字”
16+
//线程main:执行“ main:数字”
17+
public static void main(String[] args) throws InterruptedException {
18+
ThreadDemo03 t1 = new ThreadDemo03();
19+
Thread th1 = new Thread(t1,"T1") ;
20+
th1.start();
21+
22+
23+
ThreadDemo03 t2 = new ThreadDemo03();
24+
Thread th2 = new Thread(t2,"T2") ;
25+
th2.start();
26+
27+
//main线程
28+
// for(int i=0;i<10;i++){
29+
// System.out.println( Thread.currentThread().getName()+":"+i );
30+
// if(i==3){
31+
//// th1.join(); //main线程正在执行时(执行到i=3时), th1强制抢夺执行
32+
// Thread.yield(); //主动礼让给其他线程
33+
// }
34+
// }
35+
36+
37+
38+
39+
40+
}
41+
}

0 commit comments

Comments
 (0)