forked from itwanger/toBeBetterJavaer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
codes/java8demo/src/main/java/com/itwanger/thread1/MyRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.itwanger.thread1; | ||
|
||
/** | ||
* @author 微信搜「沉默王二」,回复关键字 Java | ||
*/ | ||
public class MyRunnable implements Runnable { | ||
@Override | ||
public void run() { | ||
for (int i = 0; i < 10; i++) { | ||
try {//sleep会发生异常要显示处理 | ||
Thread.sleep(20);//暂停20毫秒 | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
System.out.println(Thread.currentThread().getName() + "打了:" + i + "个小兵"); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
//创建MyRunnable类 | ||
MyRunnable mr = new MyRunnable(); | ||
//创建Thread类的有参构造,并设置线程名 | ||
Thread t1 = new Thread(mr, "张飞"); | ||
Thread t2 = new Thread(mr, "貂蝉"); | ||
Thread t3 = new Thread(mr, "吕布"); | ||
|
||
t1.setDaemon(true); | ||
t2.setDaemon(true); | ||
|
||
//启动线程 | ||
t1.start(); | ||
t2.start(); | ||
t3.start(); | ||
|
||
|
||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
codes/java8demo/src/main/java/com/itwanger/thread1/MyThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.itwanger.thread1; | ||
|
||
/** | ||
* @author 微信搜「沉默王二」,回复关键字 Java | ||
*/ | ||
public class MyThread extends Thread { | ||
@Override | ||
public void run() { | ||
for (int i = 0; i < 100; i++) { | ||
System.out.println(getName() + ":打了" + i + "个小兵"); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
//创建MyThread对象 | ||
MyThread t1=new MyThread(); | ||
MyThread t2=new MyThread(); | ||
MyThread t3=new MyThread(); | ||
//设置线程的名字 | ||
t1.setName("鲁班"); | ||
t2.setName("刘备"); | ||
t3.setName("亚瑟"); | ||
//启动线程 | ||
t1.start(); | ||
t2.start(); | ||
t3.start(); | ||
} | ||
} |