Java 多线程(三)优先级、休眠(未完待续...)

1、获取线程名称、修改线程名称

		//改名前:
		//获取当前正在执行线程的详细信息
		Thread thread = Thread.currentThread();
		//打印详细信息
		System.out.println(thread);
		//打印线程的名称
		System.out.println(thread.getName());
		
		//改名后:
		//设置线程的名称
		thread.setName("王睿");
		System.out.println(thread);
		System.out.println(thread.getName());

在这里插入图片描述

2、线程的优先级

//获取当前线程
		Thread thread = new Thread();
		//获取线程的优先级
		int priority = thread.getPriority();
		//打印线程的优先级
		System.out.println(priority);
		
		//设置线程的优先级  Thread.MAX_PRIORITY相当于10
		thread.setPriority(Thread.MAX_PRIORITY);
		//获取线程的优先级
		priority = thread.getPriority();
		System.out.println(priority);

在这里插入图片描述

3、线程的休眠

		System.out.println("睡眠前:" + LocalDateTime.now());
		try {
    
    
			Thread.sleep(1000);
		} catch (InterruptedException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("睡眠后:" + LocalDateTime.now());
		
		System.out.println("睡眠前:" + LocalDateTime.now());
		try {
    
    
			Thread.sleep(1000,999999);
		} catch (InterruptedException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("睡眠后:" + LocalDateTime.now());

猜你喜欢

转载自blog.csdn.net/qq_27494201/article/details/120123902