The join method can be used to temporarily join thread execution. When thread A executes the join() method of thread B, A will wait. Waiting for B threads to finish executing, A will execute.
class Join implements Runnable{ @Override public void run() { for (int i = 0; i < 60; i++) { System.out.println(Thread.currentThread().getName()+"...."+i); } } }
public class JoinDemo { public static void main(String[] args) throws InterruptedException { Join join = new Join(); Thread t1 = new Thread(join); Thread t2 = new Thread(join); t1.start(); t2.start(); t1.join(); for (int i = 0; i < 60; i++) { System.out.println("main......"); } System.out.println("over"); } }
The main thread will only be executed after thread 0 has finished executing. It has nothing to do with whether t2 is executed or not.
Thread priority:
The priority of the thread is 5 by default and can be viewed by printing Thread.currentThread(). The lowest priority of the thread is 1 (Thread.MIN_PRIORITY) and the highest is 10 (Thread.MAX_PRIORITY); the higher the priority, the easier it is to get the execution right of the cpu .