Java多线程学习笔记(四)——Thread类中方法介绍
currentThread():返回代码正在被哪个线程调用。
public class CurrentThreadWay {
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
t.start();
}
}
public class ThreadTest extends Thread{
public ThreadTest(){
System.out.println("调用构造方法的线程是:"+Thread.currentThread().getName());
}
public void run(){
System.out.println("调用run方法的线程是:"+Thread.currentThread().getName());
}
}
运行结果:
调用构造方法的线程是:main
调用run方法的线程是:Thread-0
isAlive():判断当前线程是否处于活动状态。活动状态就是线程已经启动尚未终止,线程处于正在运行或者开始准备运行状态。
public class IsAliveTest {
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
System.out.println("t在活动状态吗?="+t.isAlive());
t.start();
System.out.println("t在活动状态吗?="+t.isAlive());
}
}
public class ThreadTest extends Thread{
public void run(){
System.out.println("run="+this.isAlive());
}
}
运行结果:
t在活动状态吗?=false
t在活动状态吗?=true
run=true
sleep():是在指定毫秒内让当前"正在运行的线程(currentThread()返回的方法)"休眠(暂停执行)
public class SleepTest {
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
System.out.println("begin="+System.currentTimeMillis());//当前时间
t.run();
System.out.println("end="+System.currentTimeMillis());//休眠后时间
}
}
public class ThreadTest extends Thread{
public void run(){
System.out.println("run threadName:"+this.currentThread().getName()+" begin...");
try {
Thread.sleep(2000);
System.out.println("run threadName:"+this.currentThread().getName()+" end...");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
begin=1492568214427
run threadName:main begin...
run threadName:main end...
end=1492568216428
修改上面代码:
public class SleepTest {
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
System.out.println("begin="+System.currentTimeMillis());
t.start();
System.out.println("end="+System.currentTimeMillis());
}
}
public class ThreadTest extends Thread{
public void run(){
System.out.println("run threadName:"+this.currentThread().getName()+" begin= "+System.currentTimeMillis());
try {
Thread.sleep(2000);
System.out.println("run threadName:"+this.currentThread().getName()+" end= "+System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
begin=1492568593345
end=1492568593345
run threadName:Thread-0 begin= 1492568593345
run threadName:Thread-0 end= 1492568595346
因为main线程和ThreadTest线程是异步执行的,所以首先先打印main线程的begin和end,在运行ThereadTest,打印run begin和run end相关信息。
getId():获取当前线程的唯一标识符
public class CurrentThreadWay {
public static void main(String[] args) {
ThreadTest t1 = new ThreadTest();
ThreadTest t1 = new ThreadTest();
System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().getId());
t1.start();
t2.start();
System.out.println(t.getName()+" "+t1.getId());
System.out.println(t2.getName()+" "+t2.getId());
}
}
运行结果:
main 1
Thread-0 9
Thread-1 10
yield():作用是让当前线程放弃cpu执行权,让给别的线程去占用CPU执行时间,但放弃时间长短不确定,可能刚放弃,又抢回CPU的执行权。
public class YieldTest {
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
t.start();
}
}
ThreadTest中run方法为:
public void run(){
long beginTime = System.currentTimeMillis();
int count = 0;
for(int i=0;i<500000;i++){
//Thread.yield();
count = count+i;
}
long endTime = System.currentTimeMillis();
System.out.println("所需时间为:"+(endTime-beginTime)+"毫秒!");
}
结果为:2毫秒
去掉注释后为:93毫秒,CPU让给其他线程,导致结果变慢。
setPriority()和getPriority():
前者是设置线程优先级,后者是获得线程优先级。Java中优先级分为1~10级,超出这个范围会抛出Throw new IllegalArgumentException();
public class SetGetPriority {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"等级为:"
+Thread.currentThread().getPriority());
//Thread.currentThread().setPriority(6);
System.out.println(Thread.currentThread().getName()+"等级为:"
+Thread.currentThread().getPriority());
ThreadTest t = new ThreadTest();
//t.setPriority(6);
t.start();
}
}
public void run(){
System.out.println(this.currentThread().getName()+"的等级为:"+this.getPriority());
}
输出为:
main等级为:5
main等级为:5
Thread-0的等级为:5
去掉注释后结果后:
main等级为:5
main等级为:6
Thread-0的等级为:6
线程默认优先级为5,优先级越高不一定越先执行完,只能说概率更大一些。
Java多线程学习笔记(四)——Thread类中方法介绍的更多相关文章
- Java多线程学习笔记(一)——Thread类中方法介绍
currentThread():返回代码正在被哪个线程调用. public class CurrentThreadWay { public static void main(String[] args ...
- 【Java多线程系列二】Thread类的方法
Thread实现Runnable接口并实现了大量实用的方法. /* * 此方法释放CPU,但并不释放已获得的锁,其它就绪的线程将可能得到执行机会,它自己也有可能再次得到执行机会 */ public s ...
- java多线程学习笔记——详细
一.线程类 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中, ...
- JAVA多线程学习笔记(1)
JAVA多线程学习笔记(1) 由于笔者使用markdown格式书写,后续copy到blog可能存在格式不美观的问题,本文的.mk文件已经上传到个人的github,会进行同步更新.github传送门 一 ...
- Java多线程学习笔记(一)——多线程实现和安全问题
1. 线程.进程.多线程: 进程是正在执行的程序,线程是进程中的代码执行,多线程就是在一个进程中有多个线程同时执行不同的任务,就像QQ,既可以开视频,又可以同时打字聊天. 2.线程的特点: 1.运行任 ...
- Java多线程学习(四)等待/通知(wait/notify)机制
转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79690279 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...
- Java IO学习笔记四:Socket基础
作者:Grey 原文地址:Java IO学习笔记四:Socket基础 准备两个Linux实例(安装好jdk1.8),我准备的两个实例的ip地址分别为: io1实例:192.168.205.138 io ...
- 零拷贝详解 Java NIO学习笔记四(零拷贝详解)
转 https://blog.csdn.net/u013096088/article/details/79122671 Java NIO学习笔记四(零拷贝详解) 2018年01月21日 20:20:5 ...
- 《深入Java虚拟机学习笔记》- 第19章 方法的调用与返回
<深入Java虚拟机学习笔记>- 第19章 方法的调用与返回
随机推荐
- java 翻页工具类
Pagination类 package com.paic.bics.core.mybatis.page; import java.util.List; @SuppressWarnings(" ...
- [DLX反复覆盖] poj 1084 Square Destroyer
题意: n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉. 求在已经拿走一些 ...
- XMU 1125 越野车大赛 【三分】
1125: 越野车大赛 Time Limit: 500 MS Memory Limit: 64 MB Special JudgeSubmit: 8 Solved: 4[Submit][Statu ...
- 重新定义数据库历史的时刻——时间序列数据库Schwartz认为InfluxDB最有前途,Elasticsearch也不错
转自:http://www.infoq.com/cn/news/2017/04/redefine-database-history 提起VividCortex公司的创建者兼CEO Baron Schw ...
- PDO连接mysql8.0报PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers错误
安装mysql8.0之后,尝试使用php连接mysql,总是报PDO::__construct(): Server sent charset (255) unknown to the client. ...
- Linux-----Kconfig文件的简介
内核源码树的目录下都有两个文件Kconfig和Makefile.分布到各目录的Kconfig构成了一个分布式的内核配置数据库, 每个Kconfig分别描述了所属目录源文件相关的内核配置菜单.在内核配置 ...
- 17.EXTJs 中icon 与iconCls的区别及用法!
转自:https://blog.csdn.net/u013890437/article/details/38315717?utm_source=blogxgwz7
- Java 中extends与implements使用方法 (转载)
转自:http://blog.csdn.net/chen_chun_guang/article/details/6323201 初学Java语言, 代码中的extends和implements让我感到 ...
- Ruby和Swift的Range
意义 Swift Ruby [1, 2, 3, 4, 5] 1...5 1..5 [1, 2, 3, 4] 1..<5 1...5 ...
- 略微讲讲最近的 webpack 该如何加快编译
首先假设 基础的环境是有 creat-react-app 所创建的 即所有基础的loader,插件的 cache 都已经缓存了 在这种情况下想加速,真是很难 不过,有一个插件是可以观察 各个模块所花的 ...