Java multithreading (3) priority, sleep (to be continued...)

1. Get the thread name, modify the thread name

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

insert image description here

2. Thread priority

//获取当前线程
		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);

insert image description here

3. Thread sleep

		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());

Guess you like

Origin blog.csdn.net/qq_27494201/article/details/120123902