Multiple threads execute concurrently, randomly switching, calling the join () method, which freezes the thread on which the current thread is located (the general main thread) until the current thread ends and the thread resumes execution
classJointestdemoImplementsrunnable{@Override Public voidrun () { for(intx=0;x<=5;x++){ Try{Thread.Sleep (100); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+"===="+x); } } } Public classJoindemo {/** * @paramargs *@throwsinterruptedexception*/ Public Static voidMain (string[] args)throwsinterruptedexception {Jointestdemo Join=NewJointestdemo (); Thread T1=NewThread (join); Thread T2=NewThread (join); T1.start (); T2.start (); //the above two sub-threads are executed alternately, the main thread freezes, and the T1 is thawed out.T1.join (); //Show main thread for(intx=0;x<=5;x++) {Thread.Sleep (100); System.out.println (Thread.CurrentThread (). GetName ()+"===="+x); } }}
Thread priority, call the setpriority () method of the thread object to set the priority, parameters:1,5 , the most obvious;thread.max_priority,thread.min_priority ,thread.norm_priority
Call Thread.yield (); can temporarily release execution, to achieve the purpose of the average running of the thread
[Javase] Multithreading (Join method)