作为一个Java小白,持续学习是不可避免的,近期在学习多线程的一些知识,所以准备写几篇关于多线程的东西,做个知识记录,防止知识遗忘(头发渐少,大脑健忘)了。
这篇文章先简单实验线程的一些简单方法,比如sleep,getName,以供以后了解与使用。
目录
接下来直接看几个简单的Thread类的方法。(注:此篇文章的方法大部分都在中文javaAPI中,http://tool.oschina.net/apidocs/apidoc?api=jdk-zh)。
1:线程简单方法
/**
* @Author: Java小学森
* @Date: 2019/3/23 10:52
* @Description:简单线程方法
**/
public class testThread extends Thread {
private String threadNm;
public testThread(String threadNm) {
this.threadNm = threadNm;
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(threadNm + "显示:" + i);
try {
// sleep,让线程暂停X毫秒,注意,并不保证严格在X毫秒之后会重新运行此线程,
// 而是重新获得可运行资格,等待获取CPU资源。
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Main {
public static void main(String[] args) {
testThread thread1 = new testThread("线程A");
testThread thread2 = new testThread("线程B");
thread1.setName("wlc-thread");
thread1.start();
thread2.start();
// 得到当前线程的名称
System.out.println("线程的名称,getName--->" + thread1.getName());
// 得到当前线程的标识符
System.out.println("线程的标识符,getId--->" + thread1.getId());
// 得到当前线程的优先级,从1到10,可以设置,默认5。
System.out.println("线程的优先级,getPriority--->" + thread2.getPriority());
// 得到当前线程的运行状态,具体的状态请见我的上一篇博客《多线程-(一)-多线程概述》
System.out.println("线程的状态,getState--->" + thread2.getState());
// 线程名称、优先级和线程组
System.out.println("线程的字符串表示形式,toString--->" + thread1.toString());
// 当前线程所在线程组中的线程数目,
System.out.println("前线程的线程组中活动线程的数目,activeCount--->" + thread1.activeCount());
// 见目录
System.out.println("正在执行的线程对象的引用,currentThread--->" + thread1.currentThread());
// 返回ClassLoader,此处的结果为sun.misc.Launcher$AppClassLoader,
// 关于为啥是“AppClassLoader”加载器,可以查看我的博客《Java-JVM(四)-虚拟机类加载机制》
System.out.println("线程的上下文 ClassLoader,getContextClassLoader--->" + thread1.getContextClassLoader());
}
}
执行完成后,结果为:
线程的名称,getName--->wlc-thread
线程的标识符,getId--->11
线程的优先级,getPriority--->5
线程的状态,getState--->RUNNABLE
线程的字符串表示形式,toString--->Thread[wlc-thread,5,main]
前线程的线程组中活动线程的数目,activeCount--->4
正在执行的线程对象的引用,currentThread--->Thread[main,5,main]
线程的上下文 ClassLoader,getContextClassLoader--->sun.misc.Launcher$AppClassLoader@18b4aac2
线程A显示:0
线程B显示:0
线程A显示:1
线程B显示:1
线程A显示:2
线程B显示:2
线程B显示:3
线程A显示:3
线程A显示:4
线程B显示:4
2:线程的优先级
/**
* @Author: Java小学森
* @Date: 2019/3/7 2:05
* @Description:线程优先级实验
**/
public class testThread2 extends Thread {
private String threadNm;
public testThread2(String threadNm) {
this.threadNm = threadNm;
}
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(threadNm + "线程显示:" + i);
}
}
}
class Main2 {
public static void main(String[] args) {
testThread2 thread1 = new testThread2("A");
testThread2 thread2 = new testThread2("B");
Thread t1 = new Thread(thread1);
Thread t2 = new Thread(thread2);
t1.setPriority(7);
t2.setPriority(10);
t1.start();
t2.start();
}
}
在代码中,A线程比B线程小三个优先级,由于线程优先级打印的过多,在这我截取几个关键的部分,就可以看出来线程优先级的作用。
B线程显示:0 // B线程开始执行
B线程显示:1
B线程显示:2
...
B线程显示:104
A线程显示:0 // A线程开始执行
B线程显示:105
A线程显示:1
B线程显示:106
A线程显示:2
B线程显示:107
A线程显示:3
B线程显示:108
...
B线程显示:998
A线程显示:506
B线程显示:999 // B线程执行结束
A线程显示:507
A线程显示:508
A线程显示:509
...
A线程显示:998
A线程显示:999
3: 线程组
可以把某些线程归集在一个线程组中,线程组中可以有线程,也可以有线程组,他的作用是:可以批量有效的管理线程或者线程组对象。
/**
* @Author: Java小学森
* @Date: 2019/3/7 2:05
* @Description: 线程组
**/
public class testThread extends Thread {
private String threadNm;
public testThread(ThreadGroup threadGroup,String threadNm) {
super(threadGroup, threadNm);
this.threadNm = threadNm;
}
private int i = 0;
public void run() {
while (!this.isInterrupted()) {
}
System.out.println(threadNm + "线程结束");
}
}
class Main {
public static void main(String[] args) throws InterruptedException{
// 新建“java-group”线程组
ThreadGroup threadGroup = new ThreadGroup("java-group");
// 创建线程并加入线程组
testThread thread1 = new testThread(threadGroup,"A");
testThread thread2 = new testThread(threadGroup,"B");
thread1.start();
System.out.println("getName-->" + thread1.getThreadGroup().getName());
System.out.println("t1启动,线程组中线程数-->" + threadGroup.activeCount());
thread2.start();
System.out.println("t2启动,线程组中线程数-->" + threadGroup.activeCount());
sleep(90);
threadGroup.interrupt();
sleep(10);
System.out.println("interrupt后,线程组中线程数-->" + threadGroup.activeCount());
}
}
可以从下面的运行结果得知:
1:加入线程组后,只有start()之后,线程才算在线程组中;
2:线程组调用interrupt后,线程组中的线程都停止了。
getName-->java-group
t1启动,线程组中线程数-->1
t2启动,线程组中线程数-->2
A线程结束
B线程结束
interrupt后,线程组中线程数-->0
4:currentThread()
/**
* @Author: Java小学森
* @Date: 2019/3/7 2:05
* @Description:
**/
public class testThread3 extends Thread {
public void run() {
System.out.println("currentThread -->" + Thread.currentThread().getName());
System.out.println("getName -->" + this.getName());
}
}
class Main3 {
public static void main(String[] args) {
testThread3 tt1 = new testThread3();
tt1.setName("BBB");
Thread thread1 = new Thread(tt1);
thread1.setName("AAA");
thread1.start();
}
}
运行结果为:
currentThread -->AAA
getName -->BBB
因为thread1线程在运行时,实际上是运行的构造参数中的(tt1)target.run(),所以this.getName()获取的是“BBB”,而 Thread.currentThread().getName()实际上是thread.getName(),所以为“AAA”。