33.线程中常用的方法【20220811】

线程中常用的方法

1.static Thread currentThread()得到当前正在运行的线程对象
2.void start() 启动线程
3.String getName()返回该线程的名称。
        1.当没有设置线程名称的时候,系统会赋予线程一个默认的名称“Thread-0,Thread-1......”
        2.主线程【主方法的执行线程】的名称默认是“main”
4.void    setName(String name)设置线程名称

package com.wangxing.test;
public class MyThread implements Runnable{
	@Override
	public void run() {
	  for(int i=1;i<=100;i++){
		  //1.static Thread currentThread()得到当前正在运行的线程对象
		  Thread  dangqianThread=Thread.currentThread();
		  //3.String getName()返回该线程的名称。
		  String  threadname=dangqianThread.getName();
		  System.out.println(threadname+"===="+i);
	  }
	}
}

package com.wangxing.test;
public class TestMain {
	public static void main(String[] args) {
		//得到主线程的名称
		//1.static Thread currentThread()得到当前正在运行的线程对象
		//Thread mainth=Thread.currentThread();
		//String name=mainth.getName();
		//System.out.println("主线程的线程名称=="+name);
	
		//创建目标对象
		MyThread  mt=new MyThread();
		//创建线程对象
		//1.通过Thread类的构造方法设置线程名称
		//Thread th1=new Thread(mt,"线程1");
		//Thread th2=new Thread(mt,"线程2");
		Thread th1=new Thread(mt);
		Thread th2=new Thread(mt);
		//2.void	setName(String name)设置线程名称
		th1.setName("线程A");
		th2.setName("线程B");
		//启动线程
		//2.void start() 启动线程
		th1.start();
		th2.start();
	}
}

线程的优先级

线程的优先级---就是线程的执行先后。
默认情况下所有线程的优先级都是一样,都是5。
我们可以通过void    setPriority(int newPriority) 更改线程的优先级。
1.线程的优先级有10个级别,分别使用整数1~10来表示。数字越大优先级越高。
2.为了方便操作,java将10个级别有规定成3个级别,分别是最低的优先级,中等优先级,最高的优先级,并且将这3个级别封装成了静态常量    
    static int    MAX_PRIORITY 线程可以具有的最高优先级。10
    static int    MIN_PRIORITY线程可以具有的最低优先级。1
    static int    NORM_PRIORITY分配给线程的默认优先级。5
    int    getPriority() 返回线程的优先级。
3.设置线程的优先级的时候,数字越大优先级越高,数字越小优先级越低。优先级越高并不代表就一定会优先执行,只是被优先执行的几率增大,因此不要试图通过控制线程的优先级,来保证某一个线程,总是第一个执行。

package com.wangxing.test1;
public class TestMain {
	public static void main(String[] args) {
		MyThread  mt=new MyThread();
		Thread th1=new Thread(mt);
		Thread th2=new Thread(mt);
		th1.setName("线程A");
		th2.setName("线程B");
		
		//默认情况下所有线程的优先级都是一样,都是5。
		//void	setPriority(int newPriority) 更改线程的优先级。
		//参数int newPriority---就是线程优先级的级别
		//线程的优先级有10个级别,分别使用整数1~10来表示。数字越大优先级越高。
		//th1.setPriority(2);
		//th2.setPriority(8);
		//为了方便操作,java将10个级别有规定成3个级别,
		//最低的优先级----static int MIN_PRIORITY=1
		//中等优先级-----static int	 NORM_PRIORITY=5
		//最高的优先级----static int MAX_PRIORITY=10
		th1.setPriority(Thread.MIN_PRIORITY);
		th2.setPriority(Thread.MAX_PRIORITY);
		//设置线程的优先级的时候,数字越大优先级越高,数字越小优先级越低。
		//优先级越高并不代表就一定会优先执行,只是被优先执行的几率增大
		//因此不要试图通过控制线程的优先级,来保证某一个线总是第一个执行。
		th1.start();
		th2.start();
		//默认情况下所有线程的优先级都是一样,都是5。
		//int	getPriority() 返回线程的优先级。
		int thpri1=th1.getPriority();
		int thpri2=th2.getPriority();
		//得到主线程的优先级
		Thread mainThread=Thread.currentThread();
		int mainpri=mainThread.getPriority();
		System.out.println("线程A的优先级=="+thpri1);
		System.out.println("线程B的优先级=="+thpri2);
		System.out.println("主线程的优先级=="+mainpri);
	}
}

守护线程的相关操作方法

用户线程----通常情况之下我们所创建的线程都是普通线程,非守护线程,也叫用户线程。
守护线程----也叫精灵线程,当所有用户线程都执行完毕以后,自动结束运行的线程就是守护线程.[共死]
1.boolean    isDaemon() 测试该线程是否为守护线程。
2.void    setDaemon(boolean on) 将该线程标记为守护线程用户线程。
特征:当所有用户线程都执行完毕以后,无论守护线程能否可以继续运行,都要立刻停止运行。

package com.wangxing.test2;
public class MyThread implements Runnable{
	@Override
	public void run() {
	  for(int i=1;i<=100;i++){
		  //1.static Thread currentThread()得到当前正在运行的线程对象
		  Thread  dangqianThread=Thread.currentThread();
		  //3.String getName()返回该线程的名称。
		  String  threadname=dangqianThread.getName();
		  System.out.println(threadname+"===="+i);
	  }
	}
}

package com.wangxing.test2;
public class TestThread implements Runnable{
	@Override
	public void run() {
	  while(true){
		  Thread  dangqianThread=Thread.currentThread();
		  String  threadname=dangqianThread.getName();
		  System.out.println(threadname+"====守护线程");
	  }
	}
}

package com.wangxing.test2;
public class TestMain {
	public static void main(String[] args) {
		MyThread  mt=new MyThread();
		Thread th1=new Thread(mt);
		Thread th2=new Thread(mt);
		th1.setName("线程A");
		th2.setName("线程B");
		
		Thread th3=new Thread(new TestThread());
		th3.setName("线程C");
		//2.void setDaemon(boolean on) 将该线程标记为守护线程用户线程。
		th3.setDaemon(true);
		
		//1.boolean	isDaemon() 测试该线程是否为守护线程。
		//System.out.println("线程A---isDaemon=="+th1.isDaemon());
		//System.out.println("线程B---isDaemon=="+th2.isDaemon());
		//System.out.println("线程C---isDaemon=="+th3.isDaemon());
		th1.start();
		th2.start();
		th3.start();
	}
}

static void    sleep(long millis) 设置线程休眠【暂停】指定的时间【毫秒】

例如:闹钟程序
package com.wangxing.test3;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyThread implements Runnable{
	//保存闹钟时间的变量
	private  String  naozhongtime="";
	public MyThread(String  naozhongtime){
		this.naozhongtime=naozhongtime;
	}
	@Override
	public void run() {
		//设置时间格式
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		boolean  flag=true;
		while(flag){
			//static void sleep(long millis) 设置线程休眠【暂停】指定的时间【毫秒】
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			//得到当前系统时间
			Date date=new Date();
			String  time=sdf.format(date);
			System.out.println(time);
			if(time.equals(naozhongtime)){
				System.out.println("闹钟时间到!");
				flag=false;
			}
		}
	}
}


package com.wangxing.test3;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestMain {
	public static void main(String[] args) throws Exception{
		System.out.println("请输入闹钟时间:");
		BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
		String  naozhongtime=input.readLine();
		//创建目标对象
		MyThread myth=new MyThread(naozhongtime);
		//创建线程对象
		Thread  th=new Thread(myth);
		//启动线程
		th.start();
	}
}

void    interrupt() 中断线程休眠【暂停】。会进入异常【InterruptedException】。

package com.wangxing.test4;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyThread implements Runnable{
	@Override
	public void run() {
		 for(int i=1;i<=100;i++){
			  try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			  Thread  dangqianThread=Thread.currentThread();
			  String  threadname=dangqianThread.getName();
			  System.out.println(threadname+"===="+i);
			 if(i==10){
				 System.out.println("休息10秒,接着执行");
				 try {
						Thread.sleep(10000);
					} catch (InterruptedException e) {
						System.out.println("休眠时间没有结束,强制被主线程中断");
					}
			 }
		 }
	}
}

package com.wangxing.test4;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestMain {
	public static void main(String[] args) throws Exception{
		//创建目标对象
		MyThread myth=new MyThread();
		//创建线程对象
		Thread  th=new Thread(myth);
		th.setName("线程A");
		//启动线程
		th.start();
		
		for(int i=1;i<=100;i++){
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			  Thread  dangqianThread=Thread.currentThread();
			  String  threadname=dangqianThread.getName();
			  System.out.println(threadname+"===="+i);
			  if(i==25){
				  //void interrupt() 中断线程休眠【暂停】。会进入异常【InterruptedException】。
				  th.interrupt(); 
			  }
		 }
	}
}

void join(long millis)【强制线程执行】等待该线程终止的时间最长为 millis 毫秒。
注意:强制线程执行时,其他的线程不能与之交替运行。

package com.wangxing.test5;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyThread implements Runnable{
	@Override
	public void run() {
		 for(int i=1;i<=100;i++){
			  try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			  Thread  dangqianThread=Thread.currentThread();
			  String  threadname=dangqianThread.getName();
			  System.out.println(threadname+"===="+i);
		 }
	}
}

package com.wangxing.test5;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestMain {
	public static void main(String[] args) throws Exception{
		//创建目标对象
		MyThread myth=new MyThread();
		//创建线程对象
		Thread  th=new Thread(myth);
		th.setName("线程A");
		//启动线程
		th.start();
		
		for(int i=1;i<=100;i++){
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			  Thread  dangqianThread=Thread.currentThread();
			  String  threadname=dangqianThread.getName();
			  System.out.println(threadname+"===="+i);
			if(i==20){
				//void join(long millis)【强制线程执行】等待该线程终止的时间最长为 millis 毫秒。
				th.join(5000);
			} 
		 }
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值