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章 方法的调用与返回
随机推荐
- NoSQL数据库概览及其与SQL语法的比較
[文章摘要] HBase是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统.同一时候也是知名的NoSQL数据库之中的一个.NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤 ...
- 函数绑定 bind
函数拓展-bind bind实现的是:对函数绑定作用域 更改作用域的方法:call,apply,with,eval,bind call 和 apply 的比较 相同点:1.都是在使用时候(使用即执行) ...
- OpenLayers 3+Geoserver+PostGIS实现点击查询
WebGIS开发中,点击查询是最经常使用的一种查询方式,在ArcGIS api 中.这样的查询叫IdentifyTask,主要作用是前台提交參数.交ArcServer查询分析返回. 本文从开源框架的角 ...
- 浅谈MySQL Capabilities --从调研PHP mysqlnd源码细节角度认识
今天一起来研究下MySQL Capabilities,这个非常重要,如果大家有想法自己动手实现一个MySQL客户端或者Proxy工具,那么就得先了解一下这块,正好PHP 5.3以上版本由于官方为了规避 ...
- 打开与关闭Linux防火墙
1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...
- Eclipse插件开发中的选择监听机制(Selection Provider-Listener)
Eclipse插件开发中的选择监听机制(Selection Provider-Listener) 监听机制是eclipse插件开发或rcp应用开发中经常使用的技术,比方点击TableViewer或Tr ...
- redis集群状态信息维护脚本
近期在做redis相关的东西.须要把2台redis切分成16个shard. 16个主shard分布在4台128G的机器上,从shard分布在12台64G的机器上.因为机器太多,查询相关的信息不太方便. ...
- Eclipse Android环境配置
1.离线安装ADT插件,先将ZIP包下载 Help- Install New Software- Add 重启 2.WIndows -Preference设置SDK目录
- Noip模拟 Day6.12
第一题:贪吃蛇(snake) 本题其实就是判断一个有向图中有没有环,做一次拓扑排序就可以了,如果所有点都入队了,就表示没有环,否则就有环.或者就是dfs一次,每个点只需要被访问一次,这样也是O(n)的 ...
- bzoj 1497(最大权闭合子图)
1497: [NOI2006]最大获利 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 6410 Solved: 3099[Submit][Status] ...