菜鸡的Java笔记 - java 线程常用操作方法
线程常用操作方法
线程的命名操作,线程的休眠,线程的优先级
线程的所有操作方法几乎都在 Thread 类中定义好了
线程的命名和取得
从本质上来讲多线程的运行状态并不是固定的。所以来讲爱那个要想确定线程的执行,唯一的区别就在于线程的名称上
在起名的时候就应该尽可能的避免重名,或者避免修改名称
在 Thread 类中提供有如下的方法可以实现线程名称的操作:
构造方法: public Thread(Runnable target, String name)
设置名字: public final void setName(String name)
取得名字: public final String getName()
既然线程的执行本身是不确定的状态,所以如果要取得线程名字的话,那么唯一能做的就是取得当前的线程名字
所以在 Thread 类里面提供有这样的方法: public static Thread currentThread()
范例:线程的命名和取得
package cn.mysterious.study3; class MyThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ",i = " + i);
}
} } public class StudyThread { public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
new Thread(mt,"线程A").start();
new Thread(mt).start();
new Thread(mt).start();
} }
如果在设置线程对象是没有设置具体的名字,那么就采用一个默认的名字进行定义
范例:观察代码
package cn.mysterious.study3; class MyThread implements Runnable{ @Override
public void run() {
System.out.println("MyThread 线程类:" + Thread.currentThread().getName());
} } public class StudyThread { public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
new Thread(mt).start(); // 线程启动调用 run() 方法
mt.run(); // 直接通过对象调用 run() 方法
} }
/*
结果:
MyThread 线程类:main
MyThread 线程类:Thread-0
*/
MyThread 线程类:main ( mt.run(); )
MyThread 线程类:Thread-0 (new Thread(mt).start();)
线程一定是依附在进程存在的,但是现在的进程在哪里呢?
每当使用java命令在JVM上解释某一个程序执行的时候,那么都会默认的启动一个JVM的进程,而主方法只是这进程中的一个线程,所以整个程序一直都跑在线程的运行机制上
每个JVM至少会启动两个线程:主线程,GC线程
线程的休眠
如果要想让某些线程延缓执行,俺么就可以使用休眠的方式来进行处理
在 Thread 类里面提供有如下的休眠操作:
休眠方法: public static void sleep(long millis,int nanos)throws InterruptedException
如果休眠的时间没到就停止休眠了,那么就会产生中断异常
范例:观察休眠
package cn.mysterious.study3; class MyThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ",i = " + i);
}
} }
public class StudyThread { public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
new Thread(mt,"线程A").start();
new Thread(mt,"线程B").start();
new Thread(mt,"线程C").start();
} }
以上的代码执行中感觉像是所有的线程对象都同时休眠了。但是严格来讲不是同时,是有先后顺序的,只不过这个顺序小一点而已
package cn.mysterious.study3; class MyThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ",i = " + i);
}
} }
public class StudyThread { public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
Thread t = new Thread(mt,"线程A");
t.start();
Thread.sleep(2000);
t.interrupt(); // 中断 }
}
后续会使用休眠来进行线程的分析
线程的优先级
从理论上来讲优先级越高的线程越有可能先执行。而在 Thread 类里面定义有一下的优先级操作方法:
设置优先级: public final void setPriority(int newPriority)
取得优先级: public final int getPriority()
而对于优先级一共定义有三种:
最高优先级: public static final int MAX_PRIORITY: 10
中等优先级: public static final int NORM_PRIORITY: 5
最小优先级: public static final int MIN_PRIORITY: 1
范例:观察优先级
package cn.mysterious.study3; class MyThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ",i = " + i);
}
} }
public class StudyThread { public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
Thread t1 = new Thread(mt,"线程A");
Thread t2 = new Thread(mt,"线程B");
Thread t3 = new Thread(mt,"线程C");
// 设置优先级
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
t3.start();
} }
范例:主线程的优先级是什么呢?
public class StudyThread { public static void main(String[] args) throws Exception {
System.out.println(Thread.currentThread().getPriority());
} }
可以发现主线程属于中等优先级或者叫一般优先级
总计
1.线程要有名字, Thread.currentThread 取得当前线程
2.线程的休眠是有先后顺序的
3.理论上线程的优先级越高越有可能先执行
最新文章
- 锋利的jquery-读书笔记(一)
- 微软StockTrader应用程序
- Android开发学习之路-RecyclerView使用初探
- Learning to rank 介绍
- ZTE交换路由设备配置的备份与恢复
- IOS 关键字self,super,copy, retain, assign , readonly , readwrite, nonatomic、 @synthesize、@property、@dynamic
- Swift入门篇-集合
- boost-内存管理(scoped_array)
- hdoj 1875 畅通工程再续
- 兼容PHP和Java的des加密解密代码分享
- LightOJ 1135(线段树)
- 加密解密(2)*客户端,服务器,CA(Certificate Authority),公钥,私钥,证书,签名,验证
- (转+原)ipp ";No dlls were found in the Waterfall procedure";
- 多行文字水平垂直居中在div
- ios11,弹出层内的input框光标错位 键盘弹出时,输入信息,光标一直乱跳
- Notes : <;Hands-on ML with Sklearn &; TF>; Chapter 3
- Nginx – rewrite 配置 URL重写及301跳转原理图
- httpd配置文件详解及实例
- pytest文档18-配置文件pytest.ini
- 如何 3D 打印一个密码锁
热门文章
- 使用CEF(二)— 基于VS2019编写一个简单CEF样例
- Python爬取 | 唯美女生图片
- mysql中一半会选择什么样的字段为索引?(含索引创建删除查看公式)
- 题解 「BZOJ4919 Lydsy1706月赛」大根堆
- Spirit带你了解如何安全引入第三方资源
- Spring Cloud Gateway Route Predicate Factory 的使用
- OKhttp3的使用教程
- Matlab+Qt开发笔记(一):matlab搭建Qt开发matlib环境以及Demo测试
- [转]DDR相关的一些基础知识
- hdu 1171 Big Event in HDU(背包DP)