JAVA学习日记: 线程(3)

it2025-03-20  18

今天学习了的主要内容:

1.获取线程相关信息 2.线程的优先级 3.线程的串行 4.线程休眠

获取线程相关信息:

Thread类中提供了一个名为currentThread()的静态方法。 将这个方法写入某个线程就可以获得该线程的对象的引用。 getName()方法可以用来获得线程对象的名称。

线程的优先级:

优先级顾名思义谁的优先级高就先调用谁。 我们用数字1~10来表示优先级。 主线程的缺省优先级默认是5,子线程的优先级与父线程有相同的优先级。 getPriority()获得当前线程对象的优先级。 setPriority()设置当前线程对象的优先级。 优先级的静态整型常量: Thread.MIN_PRIORITY; 优先级为1 Thread.NORM_PRIORITY; 优先级为5 Thread.MAX_PRIORITY; 优先级为10

线程的串行:

就像串联电路一样,一个线程需要前一个线程运行完毕的结果才能运行时,就出现了了线程串行化这种东西。 join()方法可以使调用它的线程先执行完毕。 线程串行相关方法: public final void join() public final void join(long millis) public final void join(long millis, int nonas)

线程休眠:

休眠方法有: public static void sleep(long millis) public static void sleep(long millis, int nanos) sleep()方法提供的暂停时间不够精确,要精确一点的话用上面第二种方法。

ThreadTest02类(测试获得线程信息,优先级):

package LessonForThread03; class Thread01 extends Thread { public void run() { Thread t1 = Thread.currentThread(); t1.setName("ZLM"); System.out.println("Thread's Name:"+t1.getName()+" Thread's ID:" +t1.getId()+" Thread's priority:"+t1.getPriority()); } } class Thread02 implements Runnable { @Override public void run() { Thread t2 = Thread.currentThread(); System.out.println("Thread's Name:"+t2.getName()+" Thread's ID:" +t2.getId()+" Thread's priority:"+t2.getPriority()); } } public class ThreadTest02 { public static void main(String[] args) { Thread main_thread = Thread.currentThread(); System.out.println("Thread's Name:"+main_thread.getName()+" Thread's ID:" +main_thread.getId()+" Thread's priority:"+main_thread.getPriority()); //--------------------------------------------------------------------------------------------------- Thread01 t11 = new Thread01(); t11.setPriority(Thread.MAX_PRIORITY); t11.start(); //--------------------------------------------------------------------------------------------------- Thread02 t21 = new Thread02(); Thread t1 = new Thread(t21,"ZYH"); t1.start(); //--------------------------------------------------------------------------------------------------- } }

ThreadTest03类(测试串行和休眠):

package LessonForThread03; class MyThread01 extends Thread { @Override public void run() { int i; for (i=0; i<10; i++) { try { Thread.sleep(200); System.out.println("This is MyThread01 run number ->" + i); } catch (InterruptedException e) { e.printStackTrace(); } } } } class MyThread02 implements Runnable { @Override public void run() { int i; for (i=0; i<10; i++) { try { Thread.sleep(200); System.out.println("This is MyThread02 run number ->" + i); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class ThreadTest03 { public static void main(String[] args) { int i; MyThread01 mt1 = new MyThread01(); try { mt1.start(); mt1.join();//先执行完MyThread01线程再去执行别的线程。 } catch (InterruptedException e1) { e1.printStackTrace(); } //---------------------------------------------------------------------------------- MyThread02 mt2 = new MyThread02(); Thread t1 = new Thread(mt2); try { t1.start(); t1.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } //---------------------------------------------------------------------------------- for (i=0; i<10; i++) { try { Thread.sleep(200); System.out.println("This is MainThread run number ->" + i); } catch (InterruptedException e) { e.printStackTrace(); } } } } 本篇部分文字来源于: 咕嘟咖啡杨海滨老师 — 《java编程语言高级特性》 在这里十分感谢老师能够给我带来学习的激情。 2020.10.21 可以转载我的学习日记但请注明出处,谢谢。 本文章是本人学习笔记,不进行任何商用!也请别拿去商用!只为记录本人学习历程。 毕
最新回复(0)