join

 

public class JoinTest {

	//thread.Join adds the specified thread to the current thread, which can merge two alternately executed threads into a sequentially executed thread.
	//For example, the Join() method of thread A is called in thread B, and thread B will not continue to be executed until thread A is executed.
	 public static void main(String[] args) throws InterruptedException {
	     int num = 3;
	     System.out.println(System.currentTimeMillis());
	     for (int i = 0; i <num; i++) {
			JoinRunnable joinRunnable = new JoinRunnable(i);
			Thread thread = new Thread(joinRunnable);
			thread.start();
			thread.join();
			//thread.join(1000);
			
		 }
	     System.out.println(System.currentTimeMillis());  
	        
	 }

	 
	public static class JoinRunnable implements Runnable{

		private int taskId;
		public JoinRunnable(int taskId) {
			this.taskId = taskId;
		}


		@Override
		public void run() {
			try {
				Thread.sleep(taskId*1000);
				
				System.out.println( "taskId="+taskId+",time="+System.currentTimeMillis());
			} catch (InterruptedException e) {
				e.printStackTrace ();
			}
			
		}
		 
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

Donate to developers 

Driven by interest, I write 免费something with joy and sweat. I hope you like my work and can support it at the same time. Of course, if you have money to support a money field (support Alipay, WeChat, and the buckle group), if you have no money to support a personal field, thank you.

 

Personal homepage : http://knight-black-bob.iteye.com/



 
 
 Thank you for your sponsorship, I will do better!

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326316105&siteId=291194637