Thread-Thread's common method

Common method one: thread name

getName();

setName();

Thread.currentThread

Get the current thread object is a static method, just call it with the class name

Two ways to add thread name

public class Thread_Demo01 {
	public static void main(String[] args) {
		A a = new A("李四"); //方式一
		a.start();
//		a.setName("张三");   //方式二
	}
}
class A extends Thread{
	public A(String name) {
		super(name);
	}
	public void run() {
		for(int i=0;i<10;i++) {
			System.out.println(getName()+"-->"+i);
		}
	}
}

Common method two: sleep-wake

sleep

The specified milliseconds to make the current thread sleep, singular unit: milliseconds, static method, can be directly called by class name

interrupt

Interrupted thread's sleep and wait states. If the interrupted thread is sleeping, an IntertuptedException will be thrown

public class Thread_Demo02 {
	public static void main(String[] args) {
		SleepThread st = new SleepThread();
		st.start();
		for(int i=0;i<50;i++) {
			System.out.println("张三正在学习-->"+i);
			if(i==20) {
				System.out.println("张三叫醒李四");
				st.interrupt();
				break;
			}
		}
	}
}
class SleepThread extends Thread{
	public void run() {
		for(int i=0;i<10;i++) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("李四睡醒了,好好学习-->"+i);
		}
	}
}

Common method three: priority

setPriority

Set priority

getpriority

Get priority

Priority constant

  • MAX_PRIORITY 10
  • MIN_PRIORITY 1
  • NORM_PRINRITY 5 (default)

Priority range: 1 ~ 10

The priority of the thread can only increase the probability of running first

public class Thread_Demo03 {
	public static void main(String[] args) {
		Priority p1 = new Priority("A");
		p1.setPriority(Thread.MAX_PRIORITY);
		p1.start();
		
		Priority p2 = new Priority("B");
		p2.setPriority(Thread.MIN_PRIORITY);
		p2.start();
		
		Priority p3 = new Priority("C");
		p3.setPriority(Thread.NORM_PRIORITY);
		p3.start();
	}
}
class Priority extends Thread{
	public Priority(String name) {
		super(name);
	}
	public void run() {
		for(int i=0;i<5;i++) {
			System.out.println(getName()+"的优先级为"+getPriority()+"-->"+i);
		}
	}
}

Common method four: courtesy-cut queue

yield

Thread's courtesy, the time of the courtesy is uncertain (not absolute courtesy)

public class Thread_Demo04 {
	public static void main(String[] args) {
		YieldThread yt1 = new YieldThread();
		yt1.start();
		YieldThread yt2 = new YieldThread();
		yt2.start();
	}
}
class YieldThread extends Thread{
	public void run() {
		long time = System.currentTimeMillis();
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<10000;i++) {
			if(getName().equals("Thread-0"))Thread.yield(); //如果是yt1执行时,实行礼让
			sb.append("yield"+i);
		}
		time = System.currentTimeMillis() - time;
		System.out.println(getName()+"耗时:"+time);
	}
}

join

Thread interruption, the current thread has grabbed the CPU occupancy rights, let other threads to interrupt the queue to execute in front of themselves, if other threads successfully interrupt the queue, then let other threads finish executing before executing

public class Thread_Demo05 {
	public static void main(String[] args) {
		JoinThread jt = new JoinThread();
		jt.start();
		for(int i=100;i>0;i--) {
			if(i==90) {
				System.out.println("-------小红让小明插队---------");
				try {
					jt.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println("小红抢票-->"+i);
		}
	}
}
class JoinThread extends Thread{
	public void run() {
		for(int i=30;i>0;i--) {
			System.out.println("小明在抢票-->"+i);
		}
		
	}
}

Common method 5: Daemon thread

setDaemon(true);

true / false default false

  • Thread classification
    • User thread: The thread's task stops after normal execution
    • Daemon thread: All user threads finish execution, even if the task in the daemon thread is not ended
public class Thread_Demo06 {
	public static void main(String[] args) {
		DaemonThread dt = new DaemonThread();
		dt.setDaemon(true); //使dt线程变成守护线程
		dt.start();
		for(int i=0;i<100;i++) {
			System.out.println("用户线程执行中-->"+i);
		}
		System.out.println("----------->"+Thread.currentThread().getName()+":"+System.currentTimeMillis());
	}
}
class DaemonThread extends Thread{
	public void run() {
		while(true) {
			System.out.println("守护线程执行中。。。。");
			System.out.println(Thread.currentThread().getName()+":"+System.currentTimeMillis());
		}
	}
}

Guess you like

Origin www.cnblogs.com/xiaokw/p/12678774.html