线程操作的常用方法
- 线程可以进行命名,在Thread类里面,构造方法有两个参数的构造函数,可以直接命名名称。
- public Thread(Runnable target, String name);
- 当然,也有设置名字和获取名称的方法,都是set和get方法。
- 然后我们看看,线程的命名规则
class MyThread implements Runnable{
@Override
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
public class 进程与线程练习 {
public static void main(String[] args) throws Exception{
MyThread ans = new MyThread();
new Thread(ans,"线程A").start();
new Thread(ans).start();
new Thread(ans,"线程b").start();
}
}
- 我们在线程里面,通过Thread.currentThread.getName()方法进行获取名称,然后输出。
- 可以看出,会输出他的名称,但是当你没有名称的时候,他也会输出你的名称,但是这是java里面自带的命名规则。分别是 Thread-0 这样,每多一个,这个0就会自加。
- 然后我们再回到一个问题,当一个程序里面有快代码特别的费时间,我们就可以利用多线程的应用,将比较费时间的代码加入到多线程里面去,这样他就不会一直的霸占着资源。
public class 进程与线程练习 {
public static void main(String[] args) throws Exception{
System.out.println("1、执行操作任务一。");
System.out.println("2、执行操作任务二。");
new Thread( ()-> {
int temp = 0;
for(int i=0;i<Integer.MAX_VALUE;i++){
temp += i;
}
System.out.println(temp);
}).start();
System.out.println("3、执行操作任务三。");
}
}
- 就如同以上代码一样,直接利用Lamda表达式直接进行多线程的编写,这样,那块比较费时间的代码就不会一直霸占着资源了。
线程的休眠处理
public class 线程的休眠处理 {
public static void main(String[] args) {
new Thread(()->{
for(int i=1;i<4;i++){
System.out.println(Thread.currentThread().getName() + ": 线程处理 : " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"线程休眠").start();
}
}
- 首先呀,线程的休眠可能会产生一个java.lang.InterruptedException错误,而且这个错误是Exception的子类,这意味着这个错误必须处理。
- 代码采用的Lamda表达式,还是很方便的。
public class 线程的休眠处理 {
public static void main(String[] args) {
Runnable ans = ()->{
for(int i=1;i<4;i++){
System.out.println(Thread.currentThread().getName() + ": 线程处理 : " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
for(int x=1;x<4;x++){
new Thread(ans ,"线程休眠" + x).start();
}
}
}
- 然后我们来看一下,多线程的休眠状态,总结过上面看,好像是同一时间进行休眠和自动叫醒的,但是其实他们还是有先后顺序的。
线程中断
- 在之前的线程休眠里面就有提到一个线程中断的异常。实际上,线程的休眠是可以被打断的,在Thread类里面就有提供这样的中断执行的处理方法。
- 判断线程是否被打断:public boolean isInterrupted();
- 中断线程执行:public void interrupte();
public class 线程的休眠处理 {
public static void main(String[] args) throws InterruptedException {
Runnable a = ()->{
System.out.println("我想睡觉了!!!");
try {
Thread.sleep(10000);
System.out.println("我他妈直接醒来了!!!");
} catch (InterruptedException e) {
System.out.println("敢打扰我睡觉,老子宰了你!!!");
}
};
Thread thread = new Thread(a);
thread.start();
Thread.sleep(1000);
if(!thread.isInterrupted()){
thread.interrupt();
}
}
}
- 这样子,我们就可以做到线程的终止了。
- 所有的线程都可以被中断,但是记住了,中断线程必须进行异常处理。
- 那么,竟然有中断,那就有强制执行。
线程的强制执行
- 当满足某种条件之后,某个线程就会一直的独占资源。直到这个线程的程序结束。
public class 线程的休眠处理 {
public static void main(String[] args) throws InterruptedException {
Thread mainthread = Thread.currentThread();
Thread ans = new Thread(()->{
for(int i=1;i<11;i++){
if(i>4){
try {
mainthread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "正在进行任务" + i);
}
},"玩耍的线程");
ans.start();
for(int i=1;i<11;i++){
System.out.println("霸道的线程" + i);
}
}
}
- 这里可以看出,通过Thread.join()方法,可以直接霸道的强制执行。
- 那么有这么霸道的强制执行,那么也会有比较人性的礼让执行。
线程的礼让执行
public class 线程的休眠处理 {
public static void main(String[] args) throws InterruptedException {
Thread mainthread = Thread.currentThread();
Thread ans = new Thread(()->{
for(int i=1;i<11;i++){
if(i%3==0){
Thread.yield();
System.out.println("我他妈直接礼让了哈!!!");
}
System.out.println(Thread.currentThread().getName() + "正在进行任务" + i);
}
},"玩耍的线程");
ans.start();
for(int i=1;i<11;i++){
System.out.println("霸道的线程" + i);
}
}
}
- 我们使用Thread.yield()方法进行礼让,但是我们每一次礼让,只会礼让一次当前的资源。
线程优先级
- 从理论上来看,优先级越高,就越有可能执行。那么里面有两个函数:
- 设置优先级:public final void setPriority(int newPriority);
- 获取优先级:public final int getPriority();
- 那么因为优先级是通过数字来表示的,所以java里面有固定的优先级数字,分为三个等级。
- 最高优先级:public static final int MAX_PRIORITY = 10;
- 中等优先级:public static final int NORM_PRIORITY = 5;
- 最低优先级:public static final int MIN_PRIORITY = 1;
- 然后我们通过代码来仔细的观察一下优先级的体验。
public class 线程的休眠处理 {
public static void main(String[] args) throws InterruptedException {
Runnable ans = ()->{
for(int i=1;i<11;i++){
System.out.println(Thread.currentThread().getName() + i);
}
};
Thread a = new Thread(ans,"线程AAAAAAAAAA");
Thread b = new Thread(ans,"线程BBBBBBBBBB");
Thread c = new Thread(ans,"线程CCCCCCCCCC");
b.setPriority(Thread.MAX_PRIORITY);
a.setPriority(Thread.MIN_PRIORITY);
c.setPriority(Thread.MIN_PRIORITY);
a.start();
b.start();
c.start();
}
}
- 这样,我们就可以直接给线程设置优先级了,但是这些优先级只是理论上的优先,不一定百分百优先他们的。
- 不管是主线程还是中途创建的线程都是中等线程优先级。NORM_PRIORITY