线程的基本操作
1.線程名稱的設(shè)置和獲取 2.sleep 3.interrupt 4.join
通過下面的代碼,來實現(xiàn)上述功能
import java.util.Date;public class JavaThreadStudy {public static void main(String[] args) {Thread t0 = new Thread(new MyThread1(),"t0");// 設(shè)置守護(hù)進(jìn)程t0.setDaemon(true);t0.start();//線程啟動后,被中斷Thread t = new Thread(new MyThread(), "yucheng");t.start();t.interrupt();//線程啟動后,joinThread t1 = new Thread(new MyThread(), "yucheng1");t1.start();System.out.println(new Date());try {t1.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(new Date());Thread t2 = new Thread(new MyThread(), "yucheng2");t2.start();System.out.println(new Date());try {t2.join(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(new Date());} }class MyThread implements Runnable {@Overridepublic void run() {// 獲取線程名字System.out.println("1.Thread Name:"+Thread.currentThread().getName());// 線程休眠try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}} }class MyThread1 implements Runnable {@Overridepublic void run() {// 獲取線程名字System.out.println("1.Thread1 Name:"+Thread.currentThread().getName());// 線程休眠try {while (true){System.out.println("dam");Thread.sleep(500);}} catch (InterruptedException e) {e.printStackTrace();}} }打印結(jié)果
?
總結(jié)
- 上一篇: Leetcode70场双周赛-第一题21
- 下一篇: 线程池实战