Skip to content

Commit

Permalink
thread
Browse files Browse the repository at this point in the history
  • Loading branch information
itwanger committed Dec 16, 2021
1 parent 3ba2717 commit d7338db
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
37 changes: 37 additions & 0 deletions codes/java8demo/src/main/java/com/itwanger/thread1/MyRunnable.java
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 codes/java8demo/src/main/java/com/itwanger/thread1/MyThread.java
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();
}
}

0 comments on commit d7338db

Please sign in to comment.