线程等待join

package com.test.base.thread;

import junit.framework.Assert;

import org.junit.Test;

public class TestThread {
	
	@Test
	public void testJoinMethod(){
		Thread t1 = new Thread(new R1(100));
		Thread t2 = new Thread(new R1(1000));
		
		t1.start();
		t2.start();
		
		
		try {
			t1.join();
			t2.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("Complete");
		while(true){
			;
		}
		
	}
	
	class R1 implements Runnable{
		
		int max_num = 100;
		
		public R1(int maxNum){
			max_num = maxNum;
		}
		
		public void run() {
			for(int i=0; i< max_num; i++){
				System.out.print((Thread.currentThread().getId()%2 == 1)? "$$" : "**" );
				System.out.println("[ Id = " + Thread.currentThread().getId() + " = " + i + "]") ;
			}
			
		}
		
	}
	
	
}


猜你喜欢

转载自dannyhz.iteye.com/blog/2235571
今日推荐